Reputation: 1
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
}}
Upvotes: 0
Views: 345
Reputation: 373
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
Upvotes: 1
Reputation: 14633
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);
Upvotes: 0