![](/img/trans.png)
[英]How would I allow a user to access a specific line in a file in python?
[英]How would i index this json response to access this specific section
我已經提供了 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)
假設這個 JSON object 被命名為json
,這將打印出每個驅動程序:
drivers = json["MRData"]["DriverTable"]["Drivers"]
for driver in drivers:
print(driver)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.