我想用“BoxDet”名称列出“BoxDet”中的所有元素。目的是这样列出来:BoxDet:ABC。。。
我的JSON的一小部分:
{
"id":1,
"name":"BoxH",
"readOnly":true,
"children":[
{
"id":100,
"name":"Box1",
"readOnly":true,
"children":[
{
"id":1003,
"name":"Box2",
"children":[
{
"id":1019,
"name":"BoxDet",
"Ids":[
"ABC",
"ABC2",
"DEF2",
"DEFHD",
"LKK"
]
}
]
}
]
}
]
}
我的问题刚刚开始,我只是不能像第一个{}那样深入。 我的代码。。。
output_json = json.load(open('root.json'))
for first in output_json:
print first
for second in first:
print second
。。。给我这样的回复:
readOnly
r
e
a
d
O
n
l
y
children
c
h
i
l
d
r
e
n
。。。等等。我甚至不能更深入地了解Box1,更不用说Box2了。我正在使用Python2.7
为此,需要一个树搜索算法:
现在可以使用此递归函数来定位所需的元素:
当你试图在一个dict上使用for循环时,没有任何特别的考虑,你只得到dict中的键
最常见的做法是使用
dict.iteritems()
(在python 3中仅使用dict.items()
)或者您可以自己获取密钥的值:
如果要遍历实体的子级,可以执行以下操作:
json模块中涉及的数据结构并不像经典字典那样工作,在经典字典中,您可以通过键访问值:
相关问题 更多 >
编程相关推荐