0

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

  • Take a look at this: [How to convert arbitrary JSON into a usable structure in Java](http://stackoverflow.com/questions/10368619/how-to-convert-arbitrary-json-into-a-usable-structure-in-java/10369166#10369166) – Greg Kopff May 01 '12 at 23:44
  • Here is the output from GSON library: topics/{DSLR={weight=1.6690950917579026, noun=1}, illuminated={weight=7.6470197510265105, noun=0}, } How can I now represent it in a JAVA object, considering multiple categories and topics source/high categories/{2_249={categoryID=249, taxID=2, score=33.43533451973102},...} – gaurav kumar May 01 '12 at 23:48

2 Answers2

1

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

rmberg
  • 373
  • 4
  • 6
  • I tried your suggestion and implemented a GSON way to parse the JSON this is the output topics/{DSLR={weight=1.6690950917579026, noun=1}, illuminated={weight=7.6470197510265105, noun=0}, } source/high categories/{2_249={categoryID=249, taxID=2, score=33.43533451973102},...} How can I now represent this in a JAVA object? – gaurav kumar May 01 '12 at 23:54
0

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);
Yusuf X
  • 14,513
  • 5
  • 35
  • 47