我尝试将 JSON 数据保存到变量中,但它在 Python 中给出了 KeyError 。这是我的代码:
import json
import requests
r = requests.get("https://api.followrel.ga/api.php?id=1")
# some JSON:
x = r.text
# parse x:
y = json.loads(x)
print(y["name"])
我得到的错误:
Traceback (most recent call last):
File "main.py", line 12, in <module>
print(y["name"])
KeyError: 'name'
最后是请求文本:
{"status":"true","message":"客户详细信息","customers":{"id":"1","username":"petyadev","email":"[电子邮件受保护] ","name":"Peter Till","bio":"见鬼\u00f3。Petya vagyok,一个 Followrel 应用程序 k\u00e9sz\u00edt\u0151je。","job":"Followrel","website":" https://www.followrel.ga","coin":"500"}}
如果有人能帮助我,我会很高兴
名称不存在于嵌套在客户字典中的 y 字典中 尝试:
print(y["customers"]["name"])