[英]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.
在打印值之前,使用isinstance()方法检查值的类型,并检查它是否为字典递归。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.