简体   繁体   中英

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. eg categories and topics can have different. How I can map it to a Java object, or any other suggestion??

"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 }}

Can you at least count on the JSON representation being consistent for each type of response / object mapping? If so, you can use the Google GSON library, and create a custom deserializer to handle the non-standard JSON representation. Here is a link to the project:

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

There is a library for that: org.json . To parse a JSONObject from a String, you say:

JSONObject jo = new JSONObject(string);

However, if the string represents an array instead of a map, use:

JSONArray ja = new JSONArray(string);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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