简体   繁体   English

将没有特定标准结构的JSON解析为Map或Java Object

[英]Parse JSON without a particular standard structure into a Map or Java Object

It will be great if I can map the json to a Java object but the structure is pretty weird, it is different for all the json I am trying to parse. 如果可以将json映射到Java对象,但是结构很怪异,这对我尝试解析的所有json都是不同的。 eg categories and topics can have different. 例如类别和主题可以有不同。 How I can map it to a Java object, or any other suggestion?? 我如何将其映射到Java对象或任何其他建议?

"name":"url", "categories": { "249":{"catID":"249","score":33.43533451973102}, "34":{"catID":"34","score":3.0000000447034836}, .,.,so on }, "topics":{ "DSLR":{"weight":1.6690950917579026,"noun":1}, "illuminated":{"weight":7.6470197510265105,"noun":0}, . "name":"url", "categories": { "249":{"catID":"249","score":33.43533451973102}, "34":{"catID":"34","score":3.0000000447034836}, .,.,so on }, "topics":{ "DSLR":{"weight":1.6690950917579026,"noun":1}, "illuminated":{"weight":7.6470197510265105,"noun":0}, . .so on }} .so on }}

Can you at least count on the JSON representation being consistent for each type of response / object mapping? 您能否至少指望每种响应/对象映射类型都一致的JSON表示形式? If so, you can use the Google GSON library, and create a custom deserializer to handle the non-standard JSON representation. 如果是这样,则可以使用Google GSON库,并创建一个自定义反序列化器来处理非标准JSON表示形式。 Here is a link to the project: 这是项目的链接:

http://code.google.com/p/google-gson/ http://code.google.com/p/google-gson/

and to the user guide on how to create custom serializers / deserializers: 以及有关如何创建自定义序列化器/反序列化器的用户指南:

https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization

There is a library for that: org.json . 有一个库:org.json。 To parse a JSONObject from a String, you say: 要从字符串解析JSONObject,您需要说:

JSONObject jo = new JSONObject(string);

However, if the string represents an array instead of a map, use: 但是,如果字符串表示数组而不是映射,请使用:

JSONArray ja = new JSONArray(string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM