![](/img/trans.png)
[英]TypeError: string indices must be integers with .json() dictionary
[英]JSON TypeError: string indices must be integers
我在以下代碼中收到錯誤“類型錯誤:字符串索引必須是整數”。
import json
import requests
url = "https://petition.parliament.uk/petitions/300139.json"
response = requests.get(url)
data = response.text
parsed = json.loads(data)
sig_count = data["attributes"]["signature_count"]
print(sig_count)
import json
url = 'https://petition.parliament.uk/petitions/300139.json'
response = requests.get(url)
data = response.text
parsed = json.loads(data)
sig_count = parsed["data"]["attributes"]["signature_count"]
print(sig_count)
您正在調用變量數據而不是解析。 過濾時您也缺少“數據”鍵。
使用json.loads()
,您需要使用新定義的變量,因為操作沒有發生。 data` 是原始形式的 json,解釋為字符串。
嘗試:
parsed['attributes']['signature_count']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.