1

我有 JSON 格式的数据:

data = {"outfit":{"shirt":"red,"pants":{"jeans":"blue","trousers":"khaki"}}}

我正在尝试使用 InfoVis 将这些数据绘制到决策树中,因为它看起来很漂亮且具有交互性。问题是他们的图表采用这种格式的 JSON 数据:

data = {id:"nodeOutfit",
    name:"outfit",
    data:{},
    children:[{
        id:"nodeShirt",
        name:"shirt",
        data:{},
        children:[{
            id:"nodeRed",
            name:"red",
                   data:{},
                   children:[]
        }],
     },  {
         id:"nodePants",
         name:"pants",
         data:{},
         children:[{
             id:"nodeJeans",
             name:"jeans",
             data:{},
             children:[{
                    id:"nodeBlue",
                    name:"blue",
                    data:{},
                    children[]
             },{
             id:"nodeTrousers",
             name:"trousers",
             data:{},
             children:[{
                    id:"nodeKhaki",
                    name:"khaki",
                    data:{},
                    children:[]
          }
    }

请注意在每个键和值中添加了“id”、“数据”和“子项”,并将每个键和值称为“名称”。我觉得我必须编写一个递归函数来添加这些额外的值。是否有捷径可寻?

这就是我想做的,但我不确定这是否是正确的方法。循环遍历所有键和值并将它们替换为适当的:

for name, list in data.iteritems():
    for dict in list:
        for key, value in dict.items():
            #Need something here which changes the value for each key and values
            #Not sure about the syntax to change "outfit" to name:"outfit" as well as
            #adding id:"nodeOutfit", data:{}, and 'children' before the value

让我知道我是否离开。

这是他们的示例http://philogb.github.com/jit/static/v20/Jit/Examples/Spacetree/example1.html

这是数据http://philogb.github.com/jit/static/v20/Jit/Examples/Spacetree/example1.code.html

4

1 回答 1

1

一个简单的递归解决方案:

data = {"outfit":{"shirt":"red","pants":{"jeans":"blue","trousers":"khaki"}}}
import json
from collections import OrderedDict

def node(name, children):
    n = OrderedDict()
    n['id'] = 'node' + name.capitalize()
    n['name'] = name
    n['data'] = {}
    n['children'] = children
    return n

def convert(d):
    if type(d) == dict:
        return [node(k, convert(v)) for k, v in d.items()]
    else:
        return [node(d, [])]

print(json.dumps(convert(data), indent=True))

请注意,convert返回 a list,而不是 a dict,因为data也可以有多个键然后只是'outfit'

输出:

[
 {
  "id": "nodeOutfit",
  “名称”:“服装”,
  “数据”: {},
  “孩子们”: [
   {
    “id”:“节点衬衫”,
    “名称”:“衬衫”,
    “数据”: {},
    “孩子们”: [
     {
      "id": "nodeRed",
      “名称”:“红色”,
      “数据”: {},
      “孩子们”: []
     }
    ]
   },
   {
    "id": "节点裤",
    “名称”:“裤子”,
    “数据”: {},
    “孩子们”: [
     {
      "id": "nodeJeans",
      “名称”:“牛仔裤”,
      “数据”: {},
      “孩子们”: [
       {
        “id”:“nodeBlue”,
        “名称”:“蓝色”,
        “数据”: {},
        “孩子们”: []
       }
      ]
     },
     {
      "id": "nodeTrousers",
      “名称”:“裤子”,
      “数据”: {},
      “孩子们”: [
       {
        "id": "nodeKhaki",
        “名称”:“卡其色”,
        “数据”: {},
        “孩子们”: []
       }
      ]
     }
    ]
   }
  ]
 }
]
于 2012-12-05T17:06:32.690 回答