小编Dr *_*ato的帖子

json.loads()返回一个unicode对象而不是字典

我正在使用fabric从远程服务器上的文件中读取json:

from StringIO import StringIO

output = StringIO()
get(file_name, output)

output = output.getvalue()
Run Code Online (Sandbox Code Playgroud)

现在的价值output是:

'"{\\n \\"status\\": \\"failed\\", \\n \\"reason\\": \\"Record already exists.\\"\\n}"'

当我尝试使用json.loads(output)它返回unicode对象u'{\n "status": "failed", \n "reason": "Record already exists."\n}'而不是字典时,将此字符串解析为字典.

我想出了一个相当糟糕的修复方法,只需将新的unicode对象传回json.loads():

json.loads(json.loads(output))

有没有办法解决这个问题呢?

干杯

python unicode json fabric python-2.7

13
推荐指数
2
解决办法
1万
查看次数

标签 统计

fabric ×1

json ×1

python ×1

python-2.7 ×1

unicode ×1