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?