简体   繁体   English

JSON 字符串转换为 Dict

[英]JSON String conversion to Dict

I'm having trouble converting the below JSON string to type dict.我在将以下 JSON 字符串转换为 dict 类型时遇到问题。 json.loads throws an error because of the two sets of squiggles: {}, {} json.loads 由于两组波浪线而引发错误:{}、{}

test = '{"124074": "0.0944", "124111": "0.0809", "124194": "0.0788"}, {"128213": "0.39", "129043": "0.458", "129054": "0.378"}'

Any thoughts on how I fix this?关于我如何解决这个问题的任何想法? I need to be able to convert to dict so I can iterate through the keys.我需要能够转换为 dict 以便我可以遍历键。 Should the JSON string be represented in a different way? JSON 字符串是否应该以不同的方式表示?

Based on the comment from @rv.kvetch, one can get the following result根据@rv.kvetch 的评论,可以得到以下结果

>>> test = '{"124074": "0.0944", "124111": "0.0809", "124194": "0.0788"}, {"128213": "0.39", "129043": "0.458", "129054": "0.378"}'
>>> dicts = json.loads(f'[{test}]')
>>> dicts
[{'124074': '0.0944', '124111': '0.0809', '124194': '0.0788'}, {'128213': '0.39', '129043': '0.458', '129054': '0.378'}]
>>> dicts[0]
{'124074': '0.0944', '124111': '0.0809', '124194': '0.0788'}
>>> type(dicts[0])
<class 'dict'>

essentially a list of dicts is created instead of a single dict.本质上创建了一个字典列表而不是单个字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM