我需要将 JSON 数组整理成层次结构,这里我的 JSON 文件从未排序,但遵循结构:
{
"name":"Folder 2",
"id":"zRDg",
"parent":"OY00",
"type":"folder"
},
{
"name":"Folder 1",
"id":"OY00",
"type":"folder"
},
{
"name":"Folder 3",
"id":"ZDE1",
"type":"folder"
},
{
"name":"DX00025.jpg",
"id":"9Xdd",
"parent":"OY00",
"type":"jpeg"
}
进入这个:
{
"name":"Folder 1",
"id":"OY00",
"type":"folder",
"children": [{
"name":"Folder 2",
"id":"zRDg",
"type":"folder"
},
{
"name":"DX00025.jpg",
"id":"9Xdd",
"type":"jpeg"
}]
},
{
"name":"Folder 3",
"id":"ZDE1",
"type":"folder"
}
我无法弄清楚,因为我是 python 新手,我的开始(错误):
for index,item in result:
if item['parent']:
for item2 in result:
if item2['id'] == item['parent']:
item['children'] = item2
brake
这没关系,但问题是它不正确python,folder1/folder/folder3/ 不会为此工作,我需要一个递归函数