python - 在Python中将字符串转换为JSON

标签 python json python-2.7

我有这个简化数组要转换为 JSON。由于某种原因,它不起作用。

import json
arr = "[{u'symbol': u'YTLREIT', u'code': u'5109', u'name': u'YTL HOSPITALITY REIT'}]"
data = json.loads(arr)

我收到的错误,

ValueError:期望属性名称:第 1 行第 3 列(字符 2)

最佳答案

这不是有效的 JSON,但您可以使用 ast.literal_eval() 加载:

>>> from ast import literal_eval
>>> literal_eval(arr)
[{u'symbol': u'YTLREIT', u'code': u'5109', u'name': u'YTL HOSPITALITY REIT'}]

关于python - 在Python中将字符串转换为JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110263/

相关文章:

python - Python 中的奇怪错误 - 变量被视为全局而不是本地

javascript - JSON 解析抛出的意外 token 错误

django - 遇到未知标签 'with'

python - 如何获得由 scipy.cluster.hierarchy 制作的树状图的子树

Python: mechanize has no attribute 'TextControl' 错误

python - 在Python中使用ZMQ在客户端/服务器配置中进行嗅探器/监视

python - BayesSearchCV 跳过 : ValueError: Not all points are within the bounds of the space

.net - 针对.Net中的架构验证JSON

python - 在 Python 2.7 中读取大型 lz4 压缩 JSON 数据集

django - 无法在 django(使用 python-opencv)项目中找到人脸(人脸检测)