Python Rest API-通过字典对象循环
这里是Python新手 我正在查询一个API并得到一个json字符串,如下所示:Python Rest API-通过字典对象循环,python,json,rest,dictionary,Python,Json,Rest,Dictionary,这里是Python新手 我正在查询一个API并得到一个json字符串,如下所示: { "human": [ { "h": 310, "prob": 0.9588886499404907, "w": 457, "x": 487, "y": 1053 }, { "h": 283, "prob": 0.8738606572151184, "w": 455, "x":
{
"human": [
{
"h": 310,
"prob": 0.9588886499404907,
"w": 457,
"x": 487,
"y": 1053
},
{
"h": 283,
"prob": 0.8738606572151184,
"w": 455,
"x": 1078,
"y": 1074
},
{
"h": 216,
"prob": 0.8639854788780212,
"w": 414,
"x": 1744,
"y": 1159
},
{
"h": 292,
"prob": 0.7896996736526489,
"w": 442,
"x": 2296,
"y": 1088
}
]
}
for k, v in json_data.iteritems():
print "{0} : {1}".format(k, v)
def traverse(d):
for k, v in d.iteritems():
if isinstance(v, dict):
traverse(v)
else:
print "{0} : {1}".format(k, v)
traverse(json_data)
我知道了如何在python中获取dict对象
json\u data=json.load(response.text)
但是我不知道如何循环dict对象。我已经试过了,但这会反复打印出密钥,如何访问父对象和子对象
for data in json_data:
print data
for sub in data:
print sub
请参见以下示例:
print json_data['human']
>> [
{
"h": 310,
"prob": 0.9588886499404907,
"w": 457,
"x": 487,
"y": 1053
},
{
"h": 283,
"prob": 0.8738606572151184,
"w": 455,
"x": 1078,
"y": 1074
},
.
.
]
for data in json_data['human']:
print data
>> {
"h": 310,
"prob": 0.9588886499404907,
"w": 457,
"x": 487,
"y": 1053
}
{
"h": 283,
"prob": 0.8738606572151184,
"w": 455,
"x": 1078,
"y": 1074
}
.
.
for data in json_data['human']:
print data['h']
>> 310
283
要在按键之间循环,请执行以下操作:
for type_ in json_data:
print type_
for location in json_data[type_]:
print location
type
用于避免Python构建它type
。您可以使用任何您认为合适的名称。请参见以下示例:
print json_data['human']
>> [
{
"h": 310,
"prob": 0.9588886499404907,
"w": 457,
"x": 487,
"y": 1053
},
{
"h": 283,
"prob": 0.8738606572151184,
"w": 455,
"x": 1078,
"y": 1074
},
.
.
]
for data in json_data['human']:
print data
>> {
"h": 310,
"prob": 0.9588886499404907,
"w": 457,
"x": 487,
"y": 1053
}
{
"h": 283,
"prob": 0.8738606572151184,
"w": 455,
"x": 1078,
"y": 1074
}
.
.
for data in json_data['human']:
print data['h']
>> 310
283
要在按键之间循环,请执行以下操作:
for type_ in json_data:
print type_
for location in json_data[type_]:
print location
type
用于避免Python构建它type
。您可以使用任何您认为合适的名称。我认为您希望使用iteritems从字典中获取键和值,如下所示:
{
"human": [
{
"h": 310,
"prob": 0.9588886499404907,
"w": 457,
"x": 487,
"y": 1053
},
{
"h": 283,
"prob": 0.8738606572151184,
"w": 455,
"x": 1078,
"y": 1074
},
{
"h": 216,
"prob": 0.8639854788780212,
"w": 414,
"x": 1744,
"y": 1159
},
{
"h": 292,
"prob": 0.7896996736526489,
"w": 442,
"x": 2296,
"y": 1088
}
]
}
for k, v in json_data.iteritems():
print "{0} : {1}".format(k, v)
def traverse(d):
for k, v in d.iteritems():
if isinstance(v, dict):
traverse(v)
else:
print "{0} : {1}".format(k, v)
traverse(json_data)
如果您打算递归遍历字典,请尝试以下操作:
{
"human": [
{
"h": 310,
"prob": 0.9588886499404907,
"w": 457,
"x": 487,
"y": 1053
},
{
"h": 283,
"prob": 0.8738606572151184,
"w": 455,
"x": 1078,
"y": 1074
},
{
"h": 216,
"prob": 0.8639854788780212,
"w": 414,
"x": 1744,
"y": 1159
},
{
"h": 292,
"prob": 0.7896996736526489,
"w": 442,
"x": 2296,
"y": 1088
}
]
}
for k, v in json_data.iteritems():
print "{0} : {1}".format(k, v)
def traverse(d):
for k, v in d.iteritems():
if isinstance(v, dict):
traverse(v)
else:
print "{0} : {1}".format(k, v)
traverse(json_data)
我认为您希望使用iteritems从字典中获取键和值,如下所示:
{
"human": [
{
"h": 310,
"prob": 0.9588886499404907,
"w": 457,
"x": 487,
"y": 1053
},
{
"h": 283,
"prob": 0.8738606572151184,
"w": 455,
"x": 1078,
"y": 1074
},
{
"h": 216,
"prob": 0.8639854788780212,
"w": 414,
"x": 1744,
"y": 1159
},
{
"h": 292,
"prob": 0.7896996736526489,
"w": 442,
"x": 2296,
"y": 1088
}
]
}
for k, v in json_data.iteritems():
print "{0} : {1}".format(k, v)
def traverse(d):
for k, v in d.iteritems():
if isinstance(v, dict):
traverse(v)
else:
print "{0} : {1}".format(k, v)
traverse(json_data)
如果您打算递归遍历字典,请尝试以下操作:
{
"human": [
{
"h": 310,
"prob": 0.9588886499404907,
"w": 457,
"x": 487,
"y": 1053
},
{
"h": 283,
"prob": 0.8738606572151184,
"w": 455,
"x": 1078,
"y": 1074
},
{
"h": 216,
"prob": 0.8639854788780212,
"w": 414,
"x": 1744,
"y": 1159
},
{
"h": 292,
"prob": 0.7896996736526489,
"w": 442,
"x": 2296,
"y": 1088
}
]
}
for k, v in json_data.iteritems():
print "{0} : {1}".format(k, v)
def traverse(d):
for k, v in d.iteritems():
if isinstance(v, dict):
traverse(v)
else:
print "{0} : {1}".format(k, v)
traverse(json_data)
谢谢,但是“钥匙”可以是不同的类型,例如“车辆”、“人”、“建筑”等,我如何循环钥匙,以及如何循环每个钥匙的位置?对不起,如果我的原始问题不清楚。谢谢,但是“钥匙”可以是不同的类型,例如“车辆”、“人”、“建筑”等,我如何循环钥匙以及每个钥匙的循环位置?对不起,如果我的原始问题不清楚的话。对于Python 3,请使用
.items()
而不是.iteritems()
。对于Python 3,请使用.items()
而不是.iteritems()
。