简体   繁体   English

如何使用python从json格式的键中提取值

[英]How to extract value from key of json format using python

My JSON response is as follows: 我的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}]

I wanted to extract the value of "status" but getting the below error: 我想提取“状态”的值,但出现以下错误:

TypeError: 'Response' object has no attribute '__getitem__'

Code snippet: 程式码片段:

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)
print(response["status"])

Can somebody please help me to extract the value of 'status' ? 有人可以帮我提取'status'的价值吗?

You need to load the JSON first: 您需要先加载JSON:

import json

response = requests.request("GET", url, headers=headers, params=querystring)
data = json.loads(response.text)
print(data["status"])

Note that based on the JSON response sample provided, the response is a list, in which case you would have to print data[0]["status"] . 请注意,基于提供的JSON响应示例,响应是一个列表,在这种情况下,您将必须打印data[0]["status"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM