gpt4 book ai didi

python - 我如何遍历嵌套的 json 字典?

转载 作者:太空宇宙 更新时间:2023-11-04 10:11:40 26 4
gpt4 key购买 nike

我一直在尝试弄清楚如何遍历类似 json 的对象,这样我就可以通过名称获取用户 ID。

json

{
"ApiSearchResult": [
{
"totalNumberResults": 55,
"type": "User",
"searchResults": [
{
"firstName": "shashank",
"name": "0o_shashank._o0",
"uid": 81097836
},
{
"firstName": "Shahnawaz",
"name": "0shahnawaz.0",
"uid": 83697589
},
{
"firstName": "Ashu",
"name": "ashu.-3",
"uid": 83646061
},
{
"bgImage": "photoalbum_491396460_user82597906-1-jpeg.jpg",
"firstName": "Garfield",
"name": "beast.boy",
"uid": 82597906
},
{
"firstName": "Bharath",
"name": "bharath_mohan69",
"uid": 80197615
},
{
"bgImage": "photoalbum_481041410_user79819261-1-jpg.jpg",
"firstName": "Wille-ICE",
"name": "blowhole",
"uid": 79819261
}
]
}
]
}

python

def getidbyname(name):
event = response['ApiSearchResult'][0]['searchResults'][0]
for key, value in event.iteritems():
if value == name: continue
elif key == "uid":
return value

但是,这行不通,我从来没有真正处理过这么多嵌套元素。

最佳答案

def getidbyname(name): 
for i in data['ApiSearchResult'][0]['searchResults']:
if i['name'] == name:
return i['uid']

关于python - 我如何遍历嵌套的 json 字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37901044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com