我想遍历字典中的多个数组list。请建议。

提供了我尝试过的代码。    下面是字典,我需要迭代并打印键和值。

{'page': 2, 'per_page': 6, 'total': 12, 'total_pages': 2, 'data': [{'id': 7, 'email': '[email protected]', 'first_name': 'Michael', 'last_name': 'Lawson', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg'}, {'id': 8, 'email': '[email protected]', 'first_name': 'Lindsay', 'last_name': 'Ferguson', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg'}, {'id': 9, 'email': '[email protected]', 'first_name': 'Tobias', 'last_name': 'Funke', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg'}, {'id': 10, 'email': '[email protected]', 'first_name': 'Byron', 'last_name': 'Fields', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg'}, {'id': 11, 'email': '[email protected]', 'first_name': 'George', 'last_name': 'Edwards', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg'}, {'id': 12, 'email': '[email protected]', 'first_name': 'Rachel', 'last_name': 'Howell',avatar':'https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg'}]}

我的代码:

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)    

从字典中我们需要打印全部,     键     值

分析解答
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)

试试这个,这里的代码递归过程用于打印字典。 在printing值之前,isinstance()方法用于检查值的类型,如果是字典,则调用recursion。