我提供了一個 JSON 外觀示例。我試圖分別列印每個驅動程式資訊,但只能列印關鍵的“驅動程式”。
{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/current/drivers.json",
"limit": "30",
"offset": "0",
"total": "21",
"DriverTable": {
"season": "2021",
"Drivers": [
{
"driverId": "alonso",
"permanentNumber": "14",
"code": "ALO",
"url": "http://en.wikipedia.org/wiki/Fernando_Alonso",
"givenName": "Fernando",
"familyName": "Alonso",
"dateOfBirth": "1981-07-29",
"nationality": "Spanish"
},
{
"driverId": "bottas",
"permanentNumber": "77",
"code": "BOT",
"url": "http://en.wikipedia.org/wiki/Valtteri_Bottas",
"givenName": "Valtteri",
"familyName": "Bottas",
"dateOfBirth": "1989-08-28",
"nationality": "Finnish"
}
]
}
}
}
下面是一段處理 json 的代碼片段
print("The drivers for the current season are : \n\n")
succeeded = False
api_url = r"http://ergast.com/api/f1/current/drivers.json?"
response = requests.get(api_url)
if response.status_code == 200:
response_json = json.loads(response.content)
#for items in response_json:
for (k,v) in response_json.items():
for (i,j) in v.items():
for data in j:
drivers = ''
drivers = data
#print("Key:",i,"value :", j)
print(drivers)
uj5u.com熱心網友回復:
假設這個 JSON 物件名為json,這將列印出每個驅動程式:
drivers = json["MRData"]["DriverTable"]["Drivers"]
for driver in drivers:
print(driver)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/400138.html
