我的 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"]
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句