1

Consider I have a JSON string of the following format:

How do I parse this using Gson so that I can write a method to perform actions based on individual occurrences of the values and their parents?

Here's a sample JSON string, however, consider the actual JSON string to be much more complex one consisting of deeply nested subarrays.

A sample one:

    [{"Name":"First","Parent":"none","Elements":[{"One": 1, "Two": 2,"Parent":"none"}]},       
  {"Name":"Second","Parent":"First","Elements":"none"},
{"Name":"Third","Parent":"Second","Elements":"none"},
{"Name":"Fourth","Parent":"Eighth","Elements":[{"One": 1, "Two": 2,"Parent":"Tenth"}]},
{"Name":"Fifth","Parent":"none","Elements":[{"One": 1, "Two": 2,"Parent":"First"}]},
{"Name":"Sixth","Parent":"Fourth","Elements":[{"One": 1, "Two": 2,"Parent":"First"}]}, 
{"Name":"Seventh","Parent":"Sixth","Elements":[{"One": 1, "Two": 2,"Parent":"Ninth"}]},
{"Name":"Eighth","Parent":"Seventh","Elements":[{"One": 1, "Two": 2,"Parent":"Tenth"}]},
{"Name":"Ninth","Parent":"Fourth","Elements":[{"One": 1, "Two": 2,"Parent":"Eighth"}]},
{"Name":"Tenth","Parent":"Third","Elements":[{"One": 1, "Two": 2,"Parent":"Second"}]},
{"Name":"Eleventh","Parent":"First","Elements":[{"One": 1, "Two": 2,"Parent":"First"}]}]
6
  • @brud You edited the question to remove the example JSON, and I find the resulting question to be ambiguous, since JSON doesn't have a standard mechanism for defining object references. Is your question specific to a particular JSON structure? If so, what is an example of it? Commented Jun 14, 2011 at 22:41
  • @Programmer Bruce: Apologies, I removed it in case of it being misleading. The JSON can be a deeply nested one, with inner elements having their containers as their parents, for example. Commented Jun 15, 2011 at 3:56
  • So, the reference from a child to a parent is identified by matching the child's "parent" element value to the parent's element name for the child? Something like this is more natural to me. {"id":"father","child":{"id":"son","parent_id":"father"}} Have you no control over the JSON structure and values? Commented Jun 15, 2011 at 4:14
  • Never mind my previous comment. It was based on the previous JSON example, the structure of which differed from the currently posted JSON example above. (You keep moving target.) Commented Jun 15, 2011 at 4:22
  • Yes and Yes. Consider a hierarchical tab view structure, where one tab is the child of another, and so on, you create a tab family, with 'tab cousins', 'tab cousins once removed', 'tab son-in-laws' etc.. Commented Jun 15, 2011 at 4:23

2 Answers 2

1

Perhaps not the answer sought:

Gson does not currently have a built in mechanism to handle bi-directional references, during serialization or deserialization (except that for serialization fields in children referencing parents can be selectively excluded*, with the resulting JSON then lacking the parent reference data in the child, or custom serialization processing can replace the parent reference with a new JSON element, and for deserialization, it's possible to implement custom processing along the lines M.J. described).

Jackson does.

*The mechanism to exclude a field from serialization is to actually specify that all of the other fields should be included. See the @Expose documentation for details.

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

Comments

0

For this sort of JSON String, after conversion to a certain level you need to manually set the parent in the objects. The JSON Conversion can help you with simplar level of hierarchy like HAS-A and IS-A, but self heirarchy is a bit tricky to use.

2 Comments

You didn't say self hierarchy is impossible to implement.
correct, It is not Impossible but almost next to impossible. In that case when u r serializing an object to JSON String, u must also serialize the complete parent object, as the referecne in JSON string doesn't works. We use object referece in Java or any other OOPS language, not in JSON. in your case u will have to replace the FirstValue & SecondValue attribute with the actual JSON String, keeping in mind that u donot repeat the hierarchy, otherwise it will creare a mess.

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.