我需要遍历JSON,并用新的密钥名称替换密钥名称以及该密钥名称中的嵌套密钥名称。 我想将桃子的名称更改为猕猴桃,将siteID更改为siteId
我尝试设置,获取,不确定如何保存吗?
"config": {
stuff: stuff
},
"precheck": {
"enabled": true,
"networks": {
"apple": {
"enabled": true,
"peach": {
"enabled": true,
"siteID": "100",
"timeout": 300
},
"watermelon": {
"enabled": true,
}
答案 0 :(得分:1)
使用弹出功能将旧值分配给新键名
A = your_dict
# changing peach => kiwi
A['precheck']['networks']['apple']['kiwi'] = A['precheck']['networks']['apple'].pop('peach')
# changing siteID => siteId
A['precheck']['networks']['apple']['kiwi']['siteId'] = A['precheck']['networks']['apple']['kiwi'].pop("siteID")