json - 处理包含多维 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']
解决方案
推荐阅读
- angular - 如何从 Angular 6 中的组件以模板驱动形式添加验证
- java - JMH基准测试中的参数列表
- javafx - javafx响应式设计全屏
- service-worker - 将事件跟踪到 ServiceWorker 中的 Google TagManager
- mysql - 将Oracle时间戳类型转换为Mysql时间戳类型是错误的
- ntp - NTP 服务器设置配置
- reactjs - 如何从 mobx-state-tree 模型中的输入存储图像?
- python - vim如何显示其内容
- c# - FireBase.NET 库的 Web API 问题
- joomla - 使用joomla JModelAdmin中的组件将自定义列值保存在数据库中