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

2
  • Take a look at this: How to convert arbitrary JSON into a usable structure in Java Commented May 1, 2012 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},...} Commented May 1, 2012 at 23:48

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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?
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.