2

I have a JSON response that looks like this:

{
    "1":{
        "id":"1",
        "user_id":"1",
        "children":[
            {
                "id":"2",
                "user_id":"2",
                "children":[
                    {
                        "id":"3",
                        "user_id":"3",
                        "children":[
                            {
                                "id":"4",
                                "user_id":"2",
                                "children":[

                                ]
                            }
                        ]
                    },
                    {
                        "id":"5",
                        "user_id":"1",
                        "children":[

                        ]
                    }
                ]
            },
            {
                "id":"6",
                "user_id":"2",
                "children":[

                ]
            }
        ]
    },
    "7":{
        "id":"7",
        "user_id":"2",
        ...
    }
}

As you can see, I have nested arrays (children). I need to loop through this JSON response, going through each nested array until it runs into an empty children array, and then takes a step back to continue to the rest of the items.

I made a model class for the response, so my current code looks like this:

for (Post post : postResponse.getData()) {
    //
}

Which obviously only iterates through the top-level items (id 1 and 7 in my case).

How can I do this?

3
  • 1
    Any reason not to just use Jackson and have a List<Person> children on a Person class? Commented Dec 31, 2015 at 7:06
  • I bet there are a dozen of duplicated questions around... Commented Dec 31, 2015 at 7:10
  • please post bit more code you tried.will helpful to extend help Commented Dec 31, 2015 at 8:02

2 Answers 2

1

you need a recursive function.

function iterateTree(object) {
  if (!object) { //recursion stop criteria || you need to implement what your stop criteria is
     return;
  }
  //doSomthingWithObject();
  iterateTree(object.children);
}

I'm sure you figure it out how to use it in java.

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

Comments

0

You may try

        public void printJsonObject(JSONObject jsonObj) {
            for (String keyStr : jsonObj.keySet()) {
                Object keyvalue = jsonObj.get(keyStr);

                //Print key and value
                System.out.println("key: "+ keyStr + " value: " + keyvalue);

                //for nested objects iteration if required
                if (keyvalue instanceof JSONObject)
                    printJsonObject((JSONObject)keyvalue);
            }

        }

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.