数据类型:
[
{'className': 'class_name1', 'studentList': [{'name': '1student_name1'}, {'name': '1student_name2'}, {'name': '1student_name3'}]},
{'className': 'class_name2', 'studentList': [{'name': '2student_name1'}, {'name': '2student_name2'}, {'name': '2student_name3'}]},
{'className': 'class_name3', 'studentList': [{'name': '3student_name1'}, {'name': '3student_name2'}, {'name': '3student_name3'}]}
]
方法一、
for mon in range(1):
class_list = []
for class_ in range(3):
mon_message = {}
student_list = []
for stu in range(3):
stu_message = {}
stu_message["name"] = str(class_ + 1)+"student_name"+str(stu+1)
student_list.append(stu_message)
mon_message["className"] = "class_name" + str(class_ + 1)
mon_message["studentList"] = student_list
class_list.append(mon_message)
print(class_list)
message = json.dumps(class_list, ensure_ascii=False, cls=DateEncoder)
print(type(message))
print(type(json.loads(message)))
for item in json.loads(message):
# print(item['className'])
# for stu in item['studentList']:
# print(stu['name'])
for key in item:
print(item[key])
输出:
[{'className': 'class_name1', 'studentList': [{'name': '1student_name1'}, {'name': '1student_name2'}, {'name': '1student_name3'}]}, {'className': 'class_name2', 'studentList': [{'name': '2student_name1'}, {'name': '2student_name2'}, {'name': '2student_name3'}]}, {'className': 'class_name3', 'studentList': [{'name': '3student_name1'}, {'name': '3student_name2'}, {'name': '3student_name3'}]}]
<class 'str'>
<class 'list'>
class_name1
[{'name': '1student_name1'}, {'name': '1student_name2'}, {'name': '1student_name3'}]
class_name2
[{'name': '2student_name1'}, {'name': '2student_name2'}, {'name': '2student_name3'}]
class_name3
[{'name': '3student_name1'}, {'name': '3student_name2'}, {'name': '3student_name3'}]
方法二:输出键,值( for k,v in item.items():print(k,v))
for mon in range(1):
class_list = []
for class_ in range(3):
mon_message = {}
student_list = []
for stu in range(3):
stu_message = {}
stu_message["name"] = str(class_ + 1)+"student_name"+str(stu+1)
student_list.append(stu_message)
mon_message["className"] = "class_name" + str(class_ + 1)
mon_message["studentList"] = student_list
class_list.append(mon_message)
print(class_list)
message = json.dumps(class_list, ensure_ascii=False, cls=DateEncoder)
print(type(message))
print(type(json.loads(message)))
for item in json.loads(message):
# print(item['className'])
# for stu in item['studentList']:
# print(stu['name'])
# for key in item:
# print(item[key])
for k,v in item.items():
print(k,v)
输出:
[{'className': 'class_name1', 'studentList': [{'name': '1student_name1'}, {'name': '1student_name2'}, {'name': '1student_name3'}]}, {'className': 'class_name2', 'studentList': [{'name': '2student_name1'}, {'name': '2student_name2'}, {'name': '2student_name3'}]}, {'className': 'class_name3', 'studentList': [{'name': '3student_name1'}, {'name': '3student_name2'}, {'name': '3student_name3'}]}]
<class 'str'>
<class 'list'>
className class_name1
studentList [{'name': '1student_name1'}, {'name': '1student_name2'}, {'name': '1student_name3'}]
className class_name2
studentList [{'name': '2student_name1'}, {'name': '2student_name2'}, {'name': '2student_name3'}]
className class_name3
studentList [{'name': '3student_name1'}, {'name': '3student_name2'}, {'name': '3student_name3'}]