[英]How to extract value from key of json format using python
我的JSON響應如下:
[{"interface":"WAN 1","status":"Active","ip":"192.168.254.3","gateway":"192.168.254.1","publicIp":"206.59.240.69","dns":"192.168.254.1","vlan":2,"usingStaticIp":false}]
我想提取“狀態”的值,但出現以下錯誤:
TypeError: 'Response' object has no attribute '__getitem__'
程式碼片段:
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
print(response["status"])
有人可以幫我提取'status'
的價值嗎?
您需要先加載JSON:
import json
response = requests.request("GET", url, headers=headers, params=querystring)
data = json.loads(response.text)
print(data["status"])
請注意,基於提供的JSON響應示例,響應是一個列表,在這種情況下,您將必須打印data[0]["status"]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.