我试图将 json 数据保存到一个变量中,但它给了我 KeyError
I tried to save json data into a variable, but it gives me KeyError
我试图将 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":"Customer Details","customers":{"id":"1","username":"petyadev","email":"petya200g@gmail.com","name":"Peter Till","bio":"Hell\u00f3. Petya vagyok, a Followrel app k\u00e9sz\u00edt\u0151je.","job":"Followrel","website":"https:\/\/www.followrel.ga","coin":"500"}}
如果有人能帮助我,我会很高兴
y 字典中不存在名称,它嵌套在客户字典中
尝试:
print(y["customers"]["name"])
我试图将 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":"Customer Details","customers":{"id":"1","username":"petyadev","email":"petya200g@gmail.com","name":"Peter Till","bio":"Hell\u00f3. Petya vagyok, a Followrel app k\u00e9sz\u00edt\u0151je.","job":"Followrel","website":"https:\/\/www.followrel.ga","coin":"500"}}
如果有人能帮助我,我会很高兴
y 字典中不存在名称,它嵌套在客户字典中 尝试:
print(y["customers"]["name"])