简体   繁体   English

Python字典JSON对象

[英]Python Dictionary JSON object

I am making a get request as shown below: 我正在发出获取请求,如下所示:

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()

My response returns a dictionary object with two keys and then multiple values inside each key. 我的响应返回一个包含两个键的字典对象,然后每个键中包含多个值。

How can I assign each of these keys and then access and assess the data inside? 如何分配每个密钥,然后访问和评估其中的数据?

I've looked online and can't seem to find anything relevant. 我在网上看过,似乎找不到任何相关的内容。

Example of JSON structure JSON结构示例

[{"id":000001,"account":000001,"name":"testName1","host":testHost1","test_color":"green"}

I have multiple records like the above 我有如上所述的多条记录

If you know that you have a test1 key, you can search for green like (assuming that your json has two keys (test1 and test2) with nested values): 如果知道您有一个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.

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