我有一个JSON
,看起来像:
[{
"lat": 43.96063343238712,
"panoid": "sffcNG69c2kdZwEuYp1htw",
"lon": 3.098330084924494
}, {
"lat": 43.96052745649745,
"panoid": "2rJPv_r0gC5FBPLZK5vHDA",
"lon": 3.098487422195691
} and so on... ]
我要为每个元素(表示成对的括号)添加一个等于的键calc
(value of "lat") - 50
我该如何更新我的JSON
?我想我应该将JSON
转换为dictionary
。但是之后如何进行?
答案 0 :(得分:2)
使用json
模块将字符串加载到列表中。遍历列表中的字典,并将属性{
"isExistsUnder": null,
"State": "",
"Value": "CustomElements",
"Parts": [
{
"isExistsUnder": null,
"State": 0,
"Value": "Rule 73",
"Parts": [
{
"isExistsUnder": null,
"State": "",
"Value": "For variable initializations",
"Parts": [
{
"isExistsUnder": null,
"State": "",
"Value": "cupis",
"Parts": [
{
"isExistsUnder": null,
"State":"",
"Value": "randomText1",
"Parts": []
},
{
"isExistsUnder": null,
"State":"",
"Value": "randomText2",
"Parts": []
},
{
"isExistsUnder": null,
"State":"",
"Value": "randomText3",
"Parts": []
}
]
}
}
}
}
设置为calc
值减去lat
。
最后,如果需要,将列表转储到带有可选50
arg的字符串中以进行漂亮的打印。
indent
给予:
import json
s = '''[{
"lat": 43.96063343238712,
"panoid": "sffcNG69c2kdZwEuYp1htw",
"lon": 3.098330084924494
}, {
"lat": 43.96052745649745,
"panoid": "2rJPv_r0gC5FBPLZK5vHDA",
"lon": 3.098487422195691
}]'''
l = json.loads(s)
for d in l:
d['calc'] = d['lat'] - 50
print(json.dumps(l, indent=4))