从JSON创建父和子

时间:2016-08-05 15:55:26

标签: javascript jquery json d3.js

我需要使用d3实现一个可折叠图。但首先,有必要解析我的json文件以创建正确的层次结构。

这是我需要转换的json文件示例:

{"likes":[{"_id":null,"category":"Music","link":"https://www.facebook.com/Brum","name":"Brum"},
{"_id":null,"category":"Music","link":"https://www.facebook.com/pan", "name":"Pan FM"},
{"_id":null,"category":"Music","link":"https://www.facebook.com/Example","name":"Example"},
{"_id":null,"category":"Books","link":"https://www.facebook.com/foo","name":"Foo"},
{"_id":null,"category":"Movies","link":"https://www.facebook.com/Titanic","name":"Titanic"}]}]}

这是我需要从我的json文件中提取的结构:

var Data = [{
  "name": "SocialProfiles",
  "parent": "null",
  "children": [{
"name": "Likes",
"parent": "SocialProfiles",
"children": [{
    "name": "Music",
    "parent": "Likes",
    "children": [{
      "parent": "Music",
      "name": "Brum"
    }, {
      "parent": "Music",
      "name": "Pan FM"
    }, {
      "parent": "Music",
      "name": "Example"
    }]
  },

  {
    "name": "Books",
    "parent": "Likes",
    "children": [{
      "parent": "Books",
      "name": "Foo"
    }, {
      "parent": "Books",
      "name": "Foo"
    }]
  }, {
    "name": "Movies",
    "parent": "Likes",
    "children": [{
      "parent": "Movies",
      "name": "Titanic"
    }]
  }
]
  }]
}];

0 个答案:

没有答案