从csv为树图制作多级嵌套json

时间:2018-03-26 14:08:01

标签: python json d3.js nested

我正在从模板制作树图。运行它所需的json是嵌套的,但也包含有关组级别的信息。我正试图弄清楚如何从json制作csv。我查了一下d3.nest但是这并没有在集团层面给你提供信息,所有的信息都留下了叶子。

新数据也更复杂(有8个级别,并非所有组都有子级),因此寻找一种解决方案,允许我递归地浏览数据并采用任何格式的嵌套。 See the csv data

我也考虑过使用python来解决这个问题,但是我对python的经验不足。

以下是json格式的示例,或此处为full file

非常感谢任何指针或策略。

{"code":"c0", "desc":"Total", "class":"", "vals":[590486, 590486, 0, 0], "level":"0", "group":"", "children":[
{"code":"c1", "desc":"EU", "class":"c1", "vals":[318029, 318029, 0, 0], "level":"1", "group":"c1", "children":[{"code":"c1_27", "desc":"Sweden", "class":"c1", "vals":[8411, 8411, 0, 0], "level":"2", "group":"c1","children":[
  {"code":"c1_27_1","desc":"angermanland","vals":[100,100,0,0]},
  {"code":"c1_27_2","desc":"blekinge","vals":[200,200,0,0]},
  {"code":"c1_27_3","desc":"bohuslan","vals":[300,300,0,0]},
  {"code":"c1_27_4","desc":"dalarna","vals":[150,150,0,0]},
  {"code":"c1_27_5","desc":"dalsland","vals":[160,160,0,0]},
  {"code":"c1_27_6","desc":"gotland","vals":[107,107,0,0]},
  {"code":"c1_27_7","desc":"gastrikland","vals":[800,800,0,0]},
  {"code":"c1_27_8","desc":"halland","vals":[900,900,0,0]},
  {"code":"c1_27_9","desc":"hasingland","vals":[1100,1100,0,0]},
  {"code":"c1_27_10","desc":"harjedalen","vals":[2100,2100,0,0]},
  {"code":"c1_27_11","desc":"jamtland","vals":[1300,1300,0,0]}

  ]}
]},
{"code":"c2", "desc":"European Free Trade Association", "class":"c2", "vals":[29492, 29492, 0, 0], "level":"1", "group":"c2", "children":[
{"code":"c2_1", "desc":"Iceland", "class":"c2", "vals":[837, 837, 0, 0], "level":"2", "group":"c2"},
{"code":"c2_2", "desc":"Norway", "class":"c2", "vals":[14885, 14885, 0, 0], "level":"2", "group":"c2"},
{"code":"c2_3", "desc":"Switzerland", "class":"c2", "vals":[13730, 13730, 0, 0], "level":"2", "group":"c2"}
]}
]}

1 个答案:

答案 0 :(得分:0)

完全归功于@oskarhane

发布the answer here from his blog

等数据开始
MSGraphSDK

并以

为目标
[
    {id: 1, title: 'hello', parent: 0},
    {id: 2, title: 'hello', parent: 0},
    {id: 3, title: 'hello', parent: 1},
    {id: 4, title: 'hello', parent: 3},
    {id: 5, title: 'hello', parent: 4},
    {id: 6, title: 'hello', parent: 4},
    {id: 7, title: 'hello', parent: 3},
    {id: 8, title: 'hello', parent: 2}
]

他使用此功能

[
    {id: 1, title: 'hello', parent: 0, children: [
        {id: 3, title: 'hello', parent: 1, children: [
            {id: 4, title: 'hello', parent: 3, children: [
                {id: 5, title: 'hello', parent: 4},
                {id: 6, title: 'hello', parent: 4}
            ]},
            {id: 7, title: 'hello', parent: 3}
        ]}
    ]},
    {id: 2, title: 'hello', parent: 0, children: [
        {id: 8, title: 'hello', parent: 2}
    ]}
]

我不得不重新调整我的数据并包含父ID,但这正是我所需要的。