【发布时间】:2021-05-05 14:22:32
【问题描述】:
如何更新json列表/数组?
j = '''[{"title": "AIG Index", "country": "AUD"}, {"title": "NFP Payrol", "country": "USD"}]'''
ff = json.loads(j)
add = {"info":"ok"}
print(ff)
[{'title': 'AIG Index', 'country': 'AUD'}, {'title': 'NFP Payrol', 'country': 'USD'}]
for i in (ff):
i.update(add)
print(ff)
[{'title': 'AIG Index', 'country': 'AUD', 'info': 'ok'}, {'title': 'NFP Payrol', 'country': 'USD', '信息':'好的'}]
如果我想根据数组更新怎么办? 使用 zip / enumerate 不适用于 json
add = [{"info":"ok"}, {"info":"not ok"}]
我得到错误, ValueError:字典更新序列元素#0 的长度为1; 2 是必需的, 如何获得结果
[{'title': 'AIG Index', 'country': 'AUD', 'info': 'ok'}, {'title': 'NFP Payrol', 'country ': 'USD', 'info': '不行'}]
【问题讨论】: