如何正确地将变量插入 python 中的 json 键
How to properly insert a variable into a json key in python
我不知道如何将变量正确输入 json 键。之后我直接在密钥上收到错误。我要输入的变量就在 ["members"] 之后,上面写着 str(uuids[count]).
import requests
import json
uuids = []
count = 0
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 in data["guild"]["members"]:
data = requests.get("https://api.hypixel.net/skyblock/profile?key=42d64fe9-677c-433b-9460-f0177e0b9ded&profile=" + str(uuids[count])).json()
catacombs.append(data["profile"]["members"][str(uuids[count])]["dungeons"]["dungeon_types"]["catacombs"]["experience"])
count += 1
print(catacombs)
感谢任何帮助。提前致谢。
嗯,好像是 json 中的一些键 dungeons
不存在。(经验也是)。你可以尝试 dict.get
然后到检查是否存在,
此外,您可以使用 zip
来迭代 uuids
和 data["guild"]["members"]
,而无需使用索引来计算下面的 them.Code:
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 键。之后我直接在密钥上收到错误。我要输入的变量就在 ["members"] 之后,上面写着 str(uuids[count]).
import requests
import json
uuids = []
count = 0
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 in data["guild"]["members"]:
data = requests.get("https://api.hypixel.net/skyblock/profile?key=42d64fe9-677c-433b-9460-f0177e0b9ded&profile=" + str(uuids[count])).json()
catacombs.append(data["profile"]["members"][str(uuids[count])]["dungeons"]["dungeon_types"]["catacombs"]["experience"])
count += 1
print(catacombs)
感谢任何帮助。提前致谢。
嗯,好像是 json 中的一些键 dungeons
不存在。(经验也是)。你可以尝试 dict.get
然后到检查是否存在,
此外,您可以使用 zip
来迭代 uuids
和 data["guild"]["members"]
,而无需使用索引来计算下面的 them.Code:
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 .... ]