首页 > 解决方案 > 处理包含多维 JSON 的列表,而不使用任何嵌套的 for 循环

问题描述

我编写了下面的代码来处理下面提到的 json_list。但它有嵌套循环,我不太满意。我希望在每个 ratePlanSchedule 和 ratePlanMasterInfo/ratePlanBasedOnRates/dynamicBaseRate 和 ratePlanCode 中获取 rateAmounts、roomTypeList。

json列表:

[
            {
                "ratePlanScheduleList": {
                    "ratePlanSchedule": [
                        {
                            "ratePlanScheduleId": {
                                "id": "3758813",
                                "type": "RateSetId"
                            },
                            "ratePlanScheduleDetail": {
                                "rateAmounts": {
                                    "onePersonRate": 100,
                                    "twoPersonRate": 100,
                                    "overrideFloorAmount": False
                                },
                                "roomTypeList": [
                                    "KKABI"
                                ],
                            }
                        },
                        {
                            "ratePlanScheduleId": {
                                "id": "4247415",
                                "type": "RateSetId"
                            },
                            "ratePlanScheduleDetail": {
                                "rateAmounts": {
                                    "onePersonRate": 100,
                                    "twoPersonRate": 100,
                                    "threePersonRate": 100,
                                    "fourPersonRate": 100,
                                    "fivePersonRate": 100,
                                    "extraPersonRate": 1,
                                    "extraChildRate": 1,
                                    "overrideFloorAmount": False
                                },
                                "roomTypeList": [
                                    "KAAE",
                                    "KKABI",
                                    "KNSM",
                                    "QQNSM",
                                    "QQQELJ",
                                    "SKBLW",
                                    "SKKABM",
                                    "SKTBAR"
                                ],
                            }
                        },
                        {
                            "ratePlanScheduleId": {
                                "id": "4255775",
                                "type": "RateSetId"
                            },
                            "ratePlanScheduleDetail": {
                                "rateAmounts": {
                                    "onePersonRate": 90,
                                    "twoPersonRate": 100,
                                    "threePersonRate": 110,
                                    "fourPersonRate": 120,
                                    "fivePersonRate": 125,
                                    "overrideFloorAmount": False
                                },
                                "roomTypeList": [
                                    "QQNSM",
                                    "QQQELJ"
                                ],
                            }
                        }
                    ],
                    "ratePlanCode": "3A"
                },
                "ratePlanMasterInfo": {
                    "ratePlanBasedOnRates": [
                        {
                            "dynamicBaseRate": {
                                "dynamicBasedOnRatePlan": "RACK",
                                "dynamicBaseAmount": -10,
                                "flatOrPercentage": "PCT",
                                "rounding": "N",
                                "compareWithRateSchedules": True
                            },
                            "basedOnRatePlanType": "DynamicBaseRate"
                        }
                    ],
                },
            
            }
            ]

我写了下面的代码来处理上面的 JSON:

for i in range(0, len(ratePlanSchedule_list)):    
                    ratePlanSchedule_sub_list = ratePlanSchedule_list[i]["ratePlanScheduleList"]"ratePlanSchedule"]
                    
                    ratePlanCode = ratePlanSchedule_list[i]["ratePlanScheduleList"]["ratePlanCode"]
                    
                    ratePlanBasedOnRates = ratePlanSchedule_list[i]['ratePlanMasterInfo']['ratePlanBasedOnRates']
                    
                    for j in range(0, len(ratePlanSchedule_sub_list)):
                        roomTypeList = ratePlanSchedule_sub_list[j]['ratePlanScheduleDetail']["roomTypeList"]
                        
                        
                        rateAmounts = ratePlanSchedule_sub_list[j]['ratePlanScheduleDetail']['rateAmounts']
                        
                        for k in range(0, len(ratePlanBasedOnRates)):
                           
                                dynamicBaseAmount = ratePlanBasedOnRates[k]['dynamicBaseRate']['dynamicBaseAmount']
                                flatOrPercentage = ratePlanBasedOnRates[k]['dynamicBaseRate']['flatOrPercentage']

标签: jsonpython-3.x

解决方案


推荐阅读