gpt4 book ai didi

python - 如果该数组中不存在键,则在 json 中搜索键添加默认值

转载 作者:太空宇宙 更新时间:2023-11-04 04:26:27 27 4
gpt4 key购买 nike

我有一个像这样的 json 数据:

{'data': [{'shares': {'count': 4}, 
'id':'226196778181357_353462548788112'},
{'shares': {'count': 5}, 'id': '226196778181357_353075852160115'},
{'shares': {'count': 47}, 'id': '226196778181357_350624229071944'},
{'shares': {'count': 3}, 'id': '226196778181357_350028335798200'},
{'shares': {'count': 5}, 'id': '226196778181357_349395549194812'},
{'shares': {'count': 6}, 'id': '226196778181357_348637945937239'},
{'shares': {'count': 3}, 'id': '226196778181357_342294163238284'},
{'id': '226196778181357_341686736632360'},
{'shares': {'count': 1}, 'id': '226196778181357_341024573365243'},
{'shares': {'count': 9}, 'id': '226196778181357_339828576818176'},
{'shares': {'count': 5}, 'id': '226196778181357_338017080332659'},
{'shares': {'count': 10}, 'id': '226196778181357_337493103718390'},
{'shares': {'count': 1}, 'id': '226196778181357_337432203724480'},
{'shares': {'count': 12}, 'id': '226196778181357_336955527105481'},
{'shares': {'count': 7}, 'id': '226196778181357_335994360534931'},
{'shares': {'count': 11}, 'id': '226196778181357_335979820536385'},
{'shares': {'count': 7}, 'id': '226196778181357_333787794088921'},
{'shares': {'count': 5}, 'id': '226196778181357_333174720816895'},
{'shares': {'count': 14}, 'id': '226196778181357_332554220878945'}]}

如果不存在,我想在数据中搜索 dict key ={'shares'} 然后添加默认字典,即 'shares': {'count': 0}

最终输出如下:

{'data': [{'shares': {'count': 4}, 
'id':'226196778181357_353462548788112'},
{'shares': {'count': 5}, 'id': '226196778181357_353075852160115'},
{'shares': {'count': 47}, 'id': '226196778181357_350624229071944'},
{'shares': {'count': 3}, 'id': '226196778181357_350028335798200'},
{'shares': {'count': 5}, 'id': '226196778181357_349395549194812'},
{'shares': {'count': 6}, 'id': '226196778181357_348637945937239'},
{'shares': {'count': 3}, 'id': '226196778181357_342294163238284'},
{'shares': {'count': 0},'id': '226196778181357_341686736632360'},
{'shares': {'count': 1}, 'id': '226196778181357_341024573365243'},
{'shares': {'count': 9}, 'id': '226196778181357_339828576818176'},
{'shares': {'count': 5}, 'id': '226196778181357_338017080332659'},
{'shares': {'count': 10}, 'id': '226196778181357_337493103718390'},
{'shares': {'count': 1}, 'id': '226196778181357_337432203724480'},
{'shares': {'count': 12}, 'id': '226196778181357_336955527105481'},
{'shares': {'count': 7}, 'id': '226196778181357_335994360534931'},
{'shares': {'count': 11}, 'id': '226196778181357_335979820536385'},
{'shares': {'count': 7}, 'id': '226196778181357_333787794088921'},
{'shares': {'count': 5}, 'id': '226196778181357_333174720816895'},
{'shares': {'count': 14}, 'id': '226196778181357_332554220878945'}]}

最佳答案

试试这个:

for e in j['data']:
if not 'shares' in e:
e['shares'] = {'count': 0}

jjson.loads(your_json)

关于python - 如果该数组中不存在键,则在 json 中搜索键添加默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com