我的 JSON 数据如下所示: [{ "coverageResponse": "100000\/500000\/1000000", "insuranceLine": "COMMERCIAL", "coverageCategory": "COVERAGE", "carriers": [{ "carrierId": "LMICO", "states": "GA, CA", "excludedStates": "PA, NY" }, { "carrierId": "CNICO", "states": "NY, PA", "excludedStates": "CA, MI, OH" }] }, { "coverageResponse": "222\/333\/111", "insuranceLine": "COMMERCIAL", "coverageCategory": "COVERAGE", "carriers": [{ "carrierId": "LMICO", "states": "GA, CA", "excludedStates": "PA, NY" }, { "carrierId": "CNICO", "states": "NY, PA", "excludedStates": "CA, MI, OH" }] }]我希望states里面excludedStates的内容carriers出现在列表中这是我的输出期望: [{ "coverageResponse": "100000\/500000\/1000000", "insuranceLine": "COMMERCIAL", "coverageCategory": "COVERAGE", "carriers": [{ "carrierId": "LMICO", "states": ["GA, CA"], "excludedStates": ["PA, NY"] }, { "carrierId": "CNICO", "states": ["NY, PA"], "excludedStates": ["CA, MI, OH"] }] }, { "coverageResponse": "222\/333\/111", "insuranceLine": "COMMERCIAL", "coverageCategory": "COVERAGE", "carriers": [{ "carrierId": "LMICO", "states": "GA, CA", "excludedStates": "PA, NY" }, { "carrierId": "CNICO", "states": "NY, PA", "excludedStates": "CA, MI, OH" }]}]
1 回答
弑天下
TA贡献1818条经验 获得超8个赞
您的问题的解决方案:
init_json = #your json
for i, external in enumerate(init_json):
for j, internal in enumerate(external['carriers']):
init_json[i]['carriers'][j]['states'] = internal['states'].split(', ')
init_json[i]['carriers'][j]['excludedStates'] = internal['excludedStates'].split(', ')
添加回答
举报
0/150
提交
取消