我有 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