提前谢谢你的帮助。 我有一个具有以下结构的csv文件:
group1,group2,group3,name,info
General,Nation,,Phil,info1
General,Nation,,Karen,info2
General,Municipality,,Bill,info3
General,Municipality,,Paul,info4
Specific,Province,,Patrick,info5
Specific,Province,,Maikel,info6
Specific,Province,Governance,Mike,info7
Specific,Province,Governance,Luke,info8
Specific,District,,Maria,info9
Specific,District,,David,info10
我需要一个嵌套的JSON用于D3或amcharts。 使用这个页面上的python脚本(https://github.com/hettmett/csv_to_json),我可以创建一个嵌套的JSON
结果如下所示:
[
{
"name" : "General",
"children" : [
{
"name" : "Nation",
"children" : [
{
"name" : "",
"children" : [
{
"name" : "Phil",
"children" : [
{
"name" : "info1"
}
]
},
{
"name" : "Karen",
"children" : [
{
"name" : "info2"
}
]
}
]
}
]
},
{
"name" : "Municipality",
"children" : [
{
"name" : "",
"children" : [
{
"name" : "Bill",
"children" : [
{
"name" : "info3"
}
]
},
{
"name" : "Paul",
"children" : [
{
"name" : "info4"
}
]
}
]
}
]
}
]
},
{
"name" : "Specific",
"children" : [
{
"name" : "Province",
"children" : [
{
"name" : "",
"children" : [
{
"name" : "Patrick",
"children" : [
{
"name" : "info5"
}
]
},
{
"name" : "Maikel",
"children" : [
{
"name" : "info6"
}
]
}
]
},
{
"name" : "Governance",
"children" : [
{
"name" : "Mike",
"children" : [
{
"name" : "info7"
}
]
},
{
"name" : "Luke",
"children" : [
{
"name" : "info8"
}
]
}
]
}
]
},
{
"name" : "District",
"children" : [
{
"name" : "",
"children" : [
{
"name" : "Maria",
"children" : [
{
"name" : "info9"
}
]
},
{
"name" : "David",
"children" : [
{
"name" : "info10"
}
]
}
]
}
]
}
]
}
]
然而,这并不完全是我所需要的。问题是某些列没有值,因此反嵌套JSON中不应包含任何子级。 像这样:
"name": "Overview",
"children": [{
"name": "General",
"children": [
{ "name": "Nation",
"children": [
{"name": "Phil", "info": "info1"},
{"name": "Karen", "info": "info2"}
]
},
{ "name": "Municipality",
"children": [
{"name": "Bill", "info": "info3"},
{"name": "Paul", "info": "info4"}
]
}
]
},
{
"name": "Specific",
"children": [
{ "name": "Province",
"children": [
{"name": "Patrick", "info": "info5"},
{"name": "Maikel", "info": "info6"},
{"name": "Governance",
"children": [
{"name": "Mike", "info": "info7"},
{"name": "Luke", "info": "info8"}
]
}
]
},
{ "name": "District",
"children": [
{"name": "Maria", "info": "info9"},
{"name": "David", "info": "info10"}
]
}
]
}
]
希望有人能帮忙
问候 斯特凡
您的“理想”结果与脚本给出的结果之间实际上存在两个有意义的差异:
{"name": "xxxxx", "info": "yyyyy"}
,而不是{"name": "xxxxx", "children": [{"name": "yyyyy"}]}
李>因此,我们可以解决这两个问题:
假设您已将
js_objs
定义为上述csv-to-json库的结果我得到的结果是
注: 只要json对象不是太深,这就可以工作。否则,将达到递归深度
只是想澄清一下,在这种情况下:
相关问题 更多 >
编程相关推荐