我可以從 json 中獲取特定值嗎?
我正在嘗試獲取 id,但我不知道該怎么做。
這是json:
{
"searchType": "Movie",
"expression": "Lord Of The Rings 2022.json",
"results": [{
"id": "tt18368278",
"resultType": "Title",
"image": "https://m.media-amazon.com/images/M/MV5BZTMwZjUzNGMtMWI3My00ZGJmLWFmYWEtYjk2YWYxYzI2NWRjXkEyXkFqcGdeQXVyODY0NzcxNw@@._V1_Ratio1.7600_AL_.jpg",
"title": "The Lord of the Rings Superfans Review the Rings of Power",
"description": "(2022 Video)"
}],
"errorMessage": ""
}
我只想獲取結果的值,但我想獲取特定的值,例如id.
這是我的代碼:
import requests
import json
movie = input("Movies:")
base_url = ("https://imdb-api.com/en/API/SearchMovie/myapi/" movie)
r = (requests.get(base_url))
b = r.json()
print(b['results'])
uj5u.com熱心網友回復:
考慮到您的 json 有效,并且要容納多個結果,您可以這樣做:
[...]
r = (requests.get(base_url))
b = r.json()
for result in b['results']:
print(result['id'])
要僅獲取一個專案(陣列中的第一項),您可以執行以下操作:
print(b['results'][0]['id'])
請求檔案:https ://requests.readthedocs.io/en/latest/
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/504594.html
上一篇:chart.js僅顯示來自jsonapi回應的第一個x和y值
下一篇:JQ中的樣本標準偏差
