3

Suppose I have a JSON array like this:

[ { "id": "429d30a1-9364-4d9a-92e0-a17e00b3afba", "children": [], "parentid": "", "name": "Expo Demo" }, { "id": "f80f1034-9110-4349-93d8-a17e00c9c317", "children": [ { "id":"b60f2c1d-368b-42c4-b0b2-a1850073e1fe", "children":[], "parentid":"f80f1034-9110-4349-93d8-a17e00c9c317", "name":"Tank" } ], "parentid": "", "name": "Fishtank" }, { "id": "fc8b0697-9406-4bf0-b79c-a185007380b8", "children": [ { "id":"5ac52894-4cb6-46c2-a05a-a18500739193", "children":[ { "id": "facb264c-0577-4627-94a1-a1850073c270", "children":[ { "id":"720472b5-189e-47f1-97a5-a18500a1b7e9", "children":[], "parentid":"facb264c-0577-4627-94a1-a1850073c270", "name":"ubSubSub" }], "parentid": "5ac52894-4cb6-46c2-a05a-a18500739193", "name": "Sub-Sub1" }], "parentid":"fc8b0697-9406-4bf0-b79c-a185007380b8", "name":"Sub" }, { "id":"4d024610-a39b-49ce-8581-a18500739a75", "children":[], "parentid":"fc8b0697-9406-4bf0-b79c-a185007380b8", "name":"Sub2" } ], "parentid": "", "name": "Herman" }, { "id": "a5b140c9-9987-4e6d-a883-a18c00726883", "children": [ { "id":"fe103303-fd5e-4cd6-81a0-a18c00733737", "children":[], "parentid":"a5b140c9-9987-4e6d-a883-a18c00726883", "name":"Contains Spaces" }], "parentid": "", "name": "Kiosk" } ] 

No I want to find a certain object based on a id and once I have that, I need its children and all its childrends children

So lets say i want to find the element with an id if 4d024610-a39b-49ce-8581-a18500739a75

That should find the Element Sub2

And now it should produce all the child elements ids witch will be:

facb264c-0577-4627-94a1-a1850073c270 720472b5-189e-47f1-97a5-a18500a1b7e9 

Let say I would do

findElementsChildren("4d024610-a39b-49ce-8581-a18500739a75") 

So i guess its two parts, first find the "parent" element. Then find its childrends childrends children etc..

Any help would be much appreciated!

5
  • Well i can find the element, but Im not sure how I would find its childrend's children. The children could be nested unlimited. I did the same kind of thing in Javascript. Commented Mar 27, 2013 at 11:55
  • Perhaps you could try using a JsonPath library interrogate the structure? Commented Mar 27, 2013 at 11:58
  • The javascript one was actually just finding one lement nested deep inside a json array/object. This takes it a step further, finding all its children. I dont know how I would do that, so the algorith Commented Mar 27, 2013 at 12:06
  • 1
    Another library that would be great to traverse such JSON 'trees' is JSON.Simple. For each JSONObject you would do a recursion call, checking whether it has the given id, if it does, then return its children with another recursion call - if necessary. Commented Mar 27, 2013 at 12:31
  • Great question, finally found similar json structur as mine. Could U please post here a real working example (You said everything worked for U), because in answer that was accepted there was no method element.get("children") Commented Apr 18, 2015 at 5:16

2 Answers 2

3

You can use recursion to solve the problem of unlimited nesting. With Gson, it would be something like the following code snippet (not tested). Other libraries will provide structures as JsonElement as well.

private JsonElement findElementsChildren(JsonElement element, String id) { if(element.isJsonObject()) { JsonObject jsonObject = element.getAsJsonObject(); if(id.equals(jsonObject.get("id").getAsString())) { return jsonObject.get("children"); } else { return findElementsChildren(element.get("children").getAsJsonArray(), id); } } else if(element.isJsonArray()) { JsonArray jsonArray = element.getAsJsonArray(); for (JsonElement childElement : jsonArray) { JsonElement result = findElementsChildren(childElement, id); if(result != null) { return result; } } } return null; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Whart is JsonElement? a JSONArray?
JsonElement can be anything, a JSON object, array or primitive (string, int, ..).
Ok I changes it upa a bit, but basically the same and it works nice! thanks
0

Based on Stefan Jansen's answer I made some changes and this is what I have now:

nestedChildren declared globaly, and before the children are searched reset to new ArrayList()

private void findAllChild(JSONArray array) throws JSONException { for ( int i=0;i<array.length();i++ ) { JSONObject json = array.getJSONObject(i); JSONArray json_array = new JSONArray(json.getString("children")); nestedChildren.add(json.getString("id")); if ( json_array.length() > 0 ) { findAllChild(json_array); } } } 

This assumes it is all Arrays, witch in my case it is

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.