[英]Python Dictionary JSON object
我正在发出获取请求,如下所示:
testHeaders = {'X-Api-Key':'myAPIKey'}
testURL = 'https://api.mytest.com/v2/test.json'
r = requests.get(testURL, headers=testHeaders)
status = r.status_code
response = r.json()
我的响应返回一个包含两个键的字典对象,然后每个键中包含多个值。
如何分配每个密钥,然后访问和评估其中的数据?
我在网上看过,似乎找不到任何相关的内容。
JSON结构示例
[{"id":000001,"account":000001,"name":"testName1","host":testHost1","test_color":"green"}
我有如上所述的多条记录
如果知道您有一个test1
键,则可以搜索green
(假设您的json有两个带有嵌套值的键(test1和test2)):
for key, value in response.items():
# key will return key test1 and test2
# value will return a tuple 'testValue':'green'
if key == 'test1':
if 'test_color' in value:
color = value[test_color]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.