2 回答
TA贡献1998条经验 获得超6个赞
我得到了将示例 JSON 转换为预期 JSON 的方法:
data = [
{ "name" : "ABC", "parent":"DEF", },
{ "name" : "DEF", "parent":"null" },
{ "name" : "new_name", "parent":"ABC" },
{ "name" : "new_name2", "parent":"ABC" },
{ "name" : "Foo", "parent":"DEF"},
{ "name" : "Bar", "parent":"null"},
{ "name" : "Chandani", "parent":"new_name", "relation": "rel", "depth": 3 },
{ "name" : "Chandani333", "parent":"new_name", "relation": "rel", "depth": 3 }
]
result = {x.get("name"):x for x in data}
#print(result)
tree = [];
for a in data:
#print(a)
if a.get("parent") in result:
parent = result[a.get("parent")]
else:
parent = ""
if parent:
if "children" not in parent:
parent["children"] = []
parent["children"].append(a)
else:
tree.append(a)
参考帮助: http: //jsfiddle.net/9FqKS/ 这是我转换为 Python 的 JavaScript 解决方案
TA贡献1804条经验 获得超8个赞
您似乎想从字典中获取值列表。
result = [value for key, value in project_cost_details.items()]
添加回答
举报