简体   繁体   中英

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

[{"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):

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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