Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中正确地将变量插入json键_Python_Json_Api_Key_Keyerror - Fatal编程技术网

如何在python中正确地将变量插入json键

如何在python中正确地将变量插入json键,python,json,api,key,keyerror,Python,Json,Api,Key,Keyerror,我不知道如何正确地将变量输入json键。之后我就直接在键上出错了。我想输入的变量就在[“members”]后面,上面写着str(uuids[count]) 任何帮助都将不胜感激。提前感谢。好吧,看来一些json中不存在密钥地下城(以及体验)。您可以尝试dict.get,然后检查它是否存在 此外,您还可以使用zip迭代uuid和data[“guild”][“members”],而无需使用索引对其进行计数。代码如下: import requests uuids = [] catacombs = [

我不知道如何正确地将变量输入json键。之后我就直接在键上出错了。我想输入的变量就在[“members”]后面,上面写着str(uuids[count])


任何帮助都将不胜感激。提前感谢。

好吧,看来一些json中不存在密钥
地下城(以及体验)。您可以尝试
dict.get
,然后检查它是否存在

此外,您还可以使用
zip
迭代
uuid
data[“guild”][“members”]
,而无需使用索引对其进行计数。代码如下:

import requests

uuids = []
catacombs = []

data = requests.get("https://api.hypixel.net/guild?key=42d64fe9-677c-433b-9460-f0177e0b9ded&id=5f1654598ea8c918612a6a43").json()
for guild in data["guild"]["members"]:
    uuids.append(guild["uuid"])

for guild, uuid in zip(data["guild"]["members"], uuids):
    data = requests.get("https://api.hypixel.net/skyblock/profile?key=42d64fe9-677c-433b-9460-f0177e0b9ded&profile=" + uuid).json()
    if data["profile"] is None:
        print(uuid, ": the profile is None")
        continue
    dungeons = data["profile"]["members"][uuid].get("dungeons")
    if dungeons is not None:
        experience = dungeons["dungeon_types"]["catacombs"].get("experience")
        if experience is not None:
            print(uuid, ":", experience)
            catacombs.append(experience)
        else:
            print(uuid, ": has dungeons but don't have experience ")
    else:
        print(uuid, ": no dungeons.")

print(catacombs)
这给了我:

0baac74f903143e49d24015d8bc3a733 : no dungeons.
031c3178bfd04099b34301185d1182f3 : no dungeons.
c1558354fe2a48dabfa47cbce58cbe14 : no dungeons.
b4a5d02f35064dda9d84b58b238d0b62 : 526339.6999999998
69eda71c66ab4020ac394ba71dc84277 : no dungeons.
6cf3bb8096d048aca54ae52b987b0359 : 90720.06666666668
be3230d820b74a44a34854936499bb23 : 152437.7166666667
24002c0cce6343f2b0a8c090719da59d : 20386.716666666656

....

[526339.6999999998, 90720.06666666668, 152437.7166666667, 20386.716666666656 .... ]

地下城在哪里?我没有在json中找到它。我无法添加图片,但我建议关闭“成员”选项卡中的许多选项卡。但是它在那里。请检查我的答案。如果有任何问题,请告诉我,如果我的答案有帮助,请通过检查答案旁边的标记来接受。很抱歉,无法提前答复。我只是想对你的帮助说声谢谢。我会根据需要进行调整,我只需要学习其中一些术语。再次感谢你的帮助!实际上,在第二次尝试之后,我注意到,一旦程序进入这个特定的“uuid”,11f3261b8afe4b72809a2e8c1122b993,它给了我一个类型错误:“NoneType”对象是不可订阅的。你知道为什么有时候会发生这种情况,但不是所有时候都会发生吗。@这似乎json并不常见。我尝试向这个页面发送请求。这给了我:
{'success':True,'profile':None}
0baac74f903143e49d24015d8bc3a733 : no dungeons.
031c3178bfd04099b34301185d1182f3 : no dungeons.
c1558354fe2a48dabfa47cbce58cbe14 : no dungeons.
b4a5d02f35064dda9d84b58b238d0b62 : 526339.6999999998
69eda71c66ab4020ac394ba71dc84277 : no dungeons.
6cf3bb8096d048aca54ae52b987b0359 : 90720.06666666668
be3230d820b74a44a34854936499bb23 : 152437.7166666667
24002c0cce6343f2b0a8c090719da59d : 20386.716666666656

....

[526339.6999999998, 90720.06666666668, 152437.7166666667, 20386.716666666656 .... ]