我試圖將 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":"[email protected]","name":"Peter Till","bio":"Hell\u00f3. Petya vagyok, a Followrel app k\u00e9sz\u00edt\u0151je.","job":"Followrel","website":"https:\/\/www.followrel.ga","coin":"500"}}
如果有人可以幫助我,我會很高興
uj5u.com熱心網友回復:
y 字典中不存在名稱,它嵌套在客戶字典中嘗試:
print(y["customers"]["name"])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438101.html
