我有一个 JSONObjects(org.json.JSONObject) 的 JSONArray(org.json.JSONArray) 像
[
{"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"},
]
这里“id”字段代表对象的唯一id,“parent_id”代表它的父对象的id。我必须将此 JSONArray 转换为另一个 JSONArray,在其中我可以将元素嵌套在其父级(类似结构的目录)中,例如
[
{"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":[]}
]},
]
任何人都可以在这里帮助我,最好的方法是什么?