JSON节点添加属性

时间:2013-08-28 02:56:29

标签: json d3.js

我有一个类似下面的JSON文件:

{
 "name": "flare",
 "children": [
  {
   "name": "analytics",
   "children": [
    {
     "name": "cluster",
     "children": [
      {"name": "AgglomerativeCluster", "size": 3938},
      {"name": "CommunityStructure", "size": 3812},
      {"name": "HierarchicalCluster", "size": 6714},
      {"name": "MergeEdge", "size": 743}
     ]
    }, ....

如您所见,端节点处有一个“size”属性。

我想将“size”属性添加到其他节点(即“analytics”和“cluster”),所以我做的是:

  {
     "name": "flare",
     "children": [
      {
       {"name": "analytics", "another_size":345},
       "children": [
        {
         {"name": "cluster", "another_size":345},
         "children": [
          {"name": "AgglomerativeCluster", "size": 3938},
          {"name": "CommunityStructure", "size": 3812},
          {"name": "HierarchicalCluster", "size": 6714},
          {"name": "MergeEdge", "size": 743}
         ]
        }, ....

但它不会这样编译。

我如何实现这一目标?我想为“another_size”下的其他节点不同地为我的节点着色。

任何帮助表示赞赏!!

1 个答案:

答案 0 :(得分:2)

试试这样:

      {
         "name": "flare",
         "children": [
          {
           {"name": "analytics", 
            "another_size":345,
           "children": [
            {
             {"name": "cluster", 
              "another_size":345,
             "children": [
              {"name": "AgglomerativeCluster", "size": 3938},
              {"name": "CommunityStructure", "size": 3812},
              {"name": "HierarchicalCluster", "size": 6714},
              {"name": "MergeEdge", "size": 743}
             ]
            }, ....

完整示例

{
    "name": "flare",
    "children": [
        {
            "name": "analytics",
            "another_size": 345,
            "children": [
                {
                    "name": "cluster",
                    "another_size": 345,
                    "children": [
                        {
                            "name": "AgglomerativeCluster",
                            "size": 3938
                        },
                        {
                            "name": "CommunityStructure",
                            "size": 3812
                        },
                        {
                            "name": "HierarchicalCluster",
                            "size": 6714
                        },
                        {
                            "name": "MergeEdge",
                            "size": 743
                        }
                    ]
                },
                {
                    "name": "cluster",
                    "another_size": 345,
                    "children": [
                        {
                            "name": "AgglomerativeCluster",
                            "size": 3938
                        },
                        {
                            "name": "CommunityStructure",
                            "size": 3812
                        },
                        {
                            "name": "HierarchicalCluster",
                            "size": 6714
                        },
                        {
                            "name": "MergeEdge",
                            "size": 743
                        }
                    ]
                }
            ]
        }
    ]
}