简体   繁体   中英

I would like to iterate over multiple array list within the dictionary and print each key and value

I would like to iterate through a multiple array list within the dictionary. Please suggest.

Provided the code that i have tried. Below is the dictionary, that i need to iterate through and print key and value.

{
    'page': 2, 
    'per_page': 6, 
    'total': 12, 
    'total_pages': 2, 
    'data': [
        {
            'id': 7, 
            'email': 'michael.lawson@reqres.in', 
            'first_name': 'Michael', 
            'last_name': 'Lawson', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg'
        }, 
        {
            'id': 8, 
            'email': 'lindsay.ferguson@reqres.in', 
            'first_name': 'Lindsay', 
            'last_name': 'Ferguson', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg'
        }, 
        {
            'id': 9, 
            'email': 'tobias.funke@reqres.in', 
            'first_name': 'Tobias', 
            'last_name': 'Funke', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg'
        }, 
        {
            'id': 10, 
            'email': 'byron.fields@reqres.in', 
            'first_name': 'Byron', 
            'last_name': 'Fields', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg'
        }, 
        {
            'id': 11, 
            'email': 'george.edwards@reqres.in', 
            'first_name': 'George', 
            'last_name': 'Edwards', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg'
        }, 
        {
            'id': 12,
            'email': 'rachel.howell@reqres.in', 4
            'first_name': 'Rachel', 
            'last_name': 'Howell',
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg'
        }
    ]
}

My code:

import json


res = requests.get('https://reqres.in/api/users?page=2')
data = res.content
print(type(data))

jsondata = json.loads(data)
print(type(jsondata))  # Turning JSON encoded data into Python objects.

jsondata = json.dumps(jsondata, indent=4)  # Serialize the json data
print(type(jsondata))
print(jsondata)

res1 = requests.get('https://reqres.in/api/users?page=2')
data1 = res1.content
print(data1)

jsondata1 = json.loads(data1)
jsondata1 = json.dumps(jsondata1)
print(jsondata1)

for key, value in jsondata.items():
    print(key, '->', value)

From the dictionary, we need to print all,

Key 
Value
import json
import requests
def printseq(seq):
    if (isinstance(seq,dict)):
        for key,values in seq.items():
            if (isinstance(values,list)):
                for i in values:
                    if (isinstance(i,dict)):
                        printseq(i)
            else:
                print(key,"->",values)
res=requests.get('https://reqres.in/api/users?page=2')
data=res.content
jsondata = json.loads(data)
printseq(jsondata)

Try this out, here in this code recursive procedure is used to print dictionary. Before printing value isinstance() method is used to check the type of value and if it is dictionary recursion is called.

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