热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

具有任意JSON密钥的JacksonObjectMapper-JacksonObjectMapperwitharbitraryJSONkeys

ImusingJackson1.9.5inanAndroidprojecttoparseJSONfiles.我在Android项目中使用Jackson1.9.5来解析JSO

I'm using Jackson 1.9.5 in an Android project to parse JSON files.

我在Android项目中使用Jackson 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 Jackson 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文件的另一个实例,这可能不正确。理想情况下,我需要一些方法让Jackson将值映射到类似NameValuePair的东西。我怀疑在这种情况下自动对象映射可能不是一个选项 - 有人可以确认或否认这个吗?

1 个解决方案

#1


18  

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。使用JsonNode很像使用树,因此您可以获取子节点,读取值等等:

InputStream ifp = mgr.open("detail_schema.json");
JsonNode root = mapper.readTree(ifp);
JsonNode fields = root.get("fields");
for (JsonNode children : fields.getElements()) {
    // ...
}

Then you'd need to build your DetailSchema object manually.

然后,您需要手动构建DetailSchema对象。

Or, you can let Jackson deserialize it as a Map, in which case you'd use your code but where MyClass would be like this:

或者,您可以让Jackson将其反序列化为Map,在这种情况下,您将使用您的代码,但MyClass将如下所示:

public class MyClass {
    public Map 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 Jackson does, maybe it will allow anything anyway...)

如果您确定输入是json中的文本,则可以将地图值键入为String。 (实际上,我不确定Jackson执行什么类型的执行,也许它无论如何都会允许......)


推荐阅读
author-avatar
钟杰辉_576
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有