[英]How can I parse such json dict?
I have a dict in json format and need to get all 'src' value from all dict. 我有一个json格式的字典,需要从所有字典获取所有“ src”值。 Dict is such format 字典就是这样的格式
{
"response": {
"165": {
"photo": {
"access_key": "25b928f75a3730a988",
"created": 1490195550,
"text": "",
"sizes": [
{
"src": "https://pp.userapi.com/xxxxx.jpg",
"height": 133,
"type": "p",
"width": 200
},
],
"pid": 456239362,
"aid": -3,
"owner_id": 14793
},
"type": "photo"
},
"78": {
"photo": {
"access_key": "2cc06244975d01b54c",
"created": 1501701707,
"text": "",
"sizes": [
{
"src": "https://pp.userapi.com/xxxxx.jpg",
"height": 412,
"type": "p",
"width": 200
},
],
"pid": 456239726,
"aid": -3,
"owner_id": 14793
},
"type": "photo"
}
}
}
I stuck with 我坚持
photos = json.load(open('photos.json'))['response']
Dont know how to use the for method in this case asdfsdfsdafsdafdsfdsfdsfdsfsdfsdafsadfsdafsdafsdafsadfs 在这种情况下,不知道如何使用for方法asdfsdfsdafsdafdsfdsfdsfdsfsdfsdafsadfsdafsdafsdafsadfs
Say x is the dictionary you provided. 说x是您提供的字典。
x = json.load(open('photos.json'))['response']
This would be the way to extract sources: 这将是提取源的方法:
sizes = [x[elem]['photo']['sizes'] for elem in x]
srcs = []
for size_list in sizes:
for size_element in size_list:
srcs.append(size_element['src'])
photos = json.load(open('photos.json'))['response']
src = [i['photo']['sizes'][0]['src'] for i in photos.values()]
src
will store a list of all the values. src
将存储所有值的列表。
PS : You could pull off a one-liner too, but it would look really messy PS :您也可以选择单线,但看起来确实很乱
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.