2

I have an JSONArray(org.json.JSONArray) of JSONObjects(org.json.JSONObject) like

[
    {"id":"abc", "parent_id":""},
    {"id":"def", "parent_id":"abc"},
    {"id":"ghi", "parent_id":""},
    {"id":"jkl", "parent_id":"abc"},
    {"id":"mno", "parent_id":"ghi"},
    {"id":"mno", "parent_id":"def"},
]

Here "id" field represents unique id of the Object and "parent_id" represents id of it's parent. I have to convert this JSONArray into another JSONArray where I can have elements nested inside their parent(directory like structure) like

[
    {"id":"abc", "parent_id":"","children":[
        {"id":"def", "parent_id":"abc","children":[
            {"id":"mno", "parent_id":"def","children":[]}
        ]},
        {"id":"jkl", "parent_id":"abc","children":[]}
    ]},
    {"id":"ghi", "parent_id":"","children":[
        {"id":"mno", "parent_id":"ghi","children":[]}
    ]},
]

Can anybody help me here what is the best possible way to do so?

0

1 Answer 1

2

You'll have something like this (pseudo code)

// Element is { id, children }
Dictionary<String, Element> elements;

for (JSONObject obj : arr) {
    if (elements.hasKey(obj.id)) {
        // Maybe you need to update your element or something here
    } else {
        // Create your element
        elements[obj.id] = new Element(obj.id);
    }

    // if the parent does not exist, create a shadow of the parent
    // (it'll get filled in with more info above if encountered later)
    if (!elements.hasKey(obj.parent)) {
        elements[obj.parent] = new Element(obj.parent);
    }

    // Add yourself to children
    elements[obj.parent].children.push(elements[obj.id]);
}

// TODO: iterate your dictionary and put it into an array, this should be straightforward
// Or if you want the root of your tree return elements[""]

I apologize in advance for not being more specific, but this should work pretty generically for whatever you want to do. Also it's not Java, but easily convertible.

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

5 Comments

Thank you Anthony but your answer will work only if there is one level (parent>children) inside the hierarchy. The children may be nested up to n levels (parent>children>grand children and so on...).
This should work approximately as posted. elements[""] should contain the root, which will have children that point at the other elements inside elements. I'll have to check my c# implementation of this when I get home (I needed to do the exact same thing except mine was folding a tree from a database)
@rahul0789: Since I don't have a way of showing it in java, here's a javascript proof of concept: jsfiddle.net/Ngd9H/1
this code only provides two level...what if i want n level of parent-child
You'll have to clarify, as written this works for however nested you want.

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.