[英]how can i get the python 'json' list, dictionary Importing mixed data
i want print, python 'json' list, dictionary Importing mixed data我想要打印,python 'json' 列表,字典导入混合数据
exam json data考试json数据
{
"data": [
{
"rows": [
{
"dimensions": [
{
"value": "title1"
}
],
"metrics": [
{
"value": "potato"
},
{
"value": "sweetpotato"
},
{
"conversion_annotation_value": []
},
{
"value": "123123"
}
]
},
]
}
],
}
As far as I know, i use try据我所知,我使用尝试
itemlist = json.loads(r.text)
for i in itemlist['data']['rows']['dimensions']:
...
for i in itemlist['data']['rows']['metrics']:
...
error, i was see错误,我看到了
TypeError: list indices must be integers or slices, not str
i want see, this json data我想看看,这个 json 数据
'title1'
'123123'
They are lists inside each dict.它们是每个字典中的列表。 So you need to access them like list elements first:
所以你需要首先像列表元素一样访问它们:
for i in itemlist['data'][0]['rows'][0]['dimensions']:
print(i.get('value'))
for i in itemlist['data'][0]['rows'][0]['metrics']:
print(i.get('value'))
Use itemlist['data']['rows'].get('dimensions', [])
instead using the .get()
property of a dict.使用
itemlist['data']['rows'].get('dimensions', [])
而不是使用字典的.get()
属性。
Or a bit more generally:或者更笼统地说:
itemlist = json.loads(r.text)
for k, v in itemlist['data']['rows']:
for i in v:
if k == 'a':
# code ....
elif k == 'b':
# code ....
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.