Python:读列表中的字典数据 [{}]

 数据类型:

[
{'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'}]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值