java 具有任意 JSON 键的 Jackson ObjectMapper
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11498499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson ObjectMapper with arbitrary JSON keys
提问by aaronsnoswell
I'm using Hymanson 1.9.5 in an Android project to parse JSON files.
我在 Android 项目中使用 Hymanson 1.9.5 来解析 JSON 文件。
So far I haven't had any problems, and can parse files fine using the following code:
到目前为止,我没有遇到任何问题,并且可以使用以下代码很好地解析文件:
AssetManager mgr = getAssets();
ObjectMapper mapper = new ObjectMapper();
try {
InputStream ifp = mgr.open("detail_schema.json");
schema = mapper.readValue(ifp, DetailSchema.class);
} catch (IOException e) {
e.printStackTrace();
}
Where the DetailSchema class consists of a mix of primitive types and classes. I'm now running into a problem where I want to parse some JSON like the following:
其中 DetailSchema 类由原始类型和类的混合组成。我现在遇到了一个问题,我想解析一些 JSON,如下所示:
"fields": {
"Suburb": "Paddington",
"State": "NSW",
"Post Code": "2074",
"Lollipop": "Foo Bar Haz"
}
Where I can't possibly know the map keys before hand (they can be user-defined). As such, I'm not sure what the associated Java class should look like.
我不可能事先知道地图键(它们可以是用户定义的)。因此,我不确定关联的 Java 类应该是什么样子。
Ie, for this example, it could look like:
即,对于这个例子,它可能看起来像:
public class MyClass {
public String Suburb;
public String State;
public String PostCode;
public String Lollipop;
}
But this may not be correct for another instance of the JSON file. Ideally I need some way for Hymanson to map values to something like a NameValuePair. I suspect that the automatic object mapping may not be an option in this case - can someone confirm or deny this?
但这对于 JSON 文件的另一个实例可能不正确。理想情况下,我需要某种方式让Hyman逊将值映射到NameValuePair 之类的东西。我怀疑在这种情况下自动对象映射可能不是一个选项 - 有人可以确认或否认这一点吗?
回答by waxwing
You have two options. Either you can use readTree in ObjectMapper, which returns a JsonNode. Working with a JsonNode
is much like working with a tree, so you can get children nodes, read values, et cetera:
你有两个选择。您可以在 ObjectMapper 中使用readTree,它返回一个JsonNode。使用 aJsonNode
就像使用树一样,因此您可以获得子节点、读取值等:
InputStream ifp = mgr.open("detail_schema.json");
JsonNode root = mapper.readTree(ifp);
JsonNode fields = root.get("fields");
for (JsonNode children : fields) {
// ...
}
Then you'd need to build your DetailSchema
object manually.
然后你需要DetailSchema
手动构建你的对象。
Or, you can let Hymanson deserialize it as a Map
, in which case you'd use your code but where MyClass
would be like this:
或者,您可以让 Hymanson 将其反序列化为 a Map
,在这种情况下,您将使用您的代码,但在哪里MyClass
会像这样:
public class MyClass {
public Map<String, Object> fields;
// getter/setters
}
You can probably type the map values as String
as well if you are sure the inputs are text in json. (Actually, I'm not sure what type enforcement Hymanson does, maybe it will allow anything anyway...)
String
如果您确定输入是 json 中的文本,您也可以键入地图值。(实际上,我不确定 Hymanson 是做什么类型的强制执行的,也许无论如何它都会允许......)