python json 加载和 unicode

标签 python json unicode utf-8 simplejson

在以下情况下,我得到了 UTF-8 编码的 HTTP 响应的结果。我想加载响应内容(JSON)。但是我不知道为什么我必须执行 2 个 json.loads 才能获得最终列表:

result = urllib2.urlopen(req).read()
print result, type(result)
#=> "[{\"pk\": 66, \"model\": \"core.job\", \"fields\": {\"customer\": 1, \"created_ts\": \"2010-03-06 06:33:36\", \"log\": 66, \"process\": 1, \"ended_ts\": null, \"state\": \"PENDING\", \"started_ts\": null}}]" <type 'str'>
ret = json.loads(result)
print ret , type(ret)
#=> [{"pk": 66, "model": "core.job", "fields": {"customer": 1, "created_ts": "2010-03-06 06:33:36", "log": 66, "process": 1, "ended_ts": null, "state": "PENDING", "started_ts": null}}] <type 'unicode'>
ret = json.loads(ret)
print ret , type(ret)
#=>[{u'pk': 66, u'model': u'core.job', u'fields': {u'customer': 1, u'created_ts': u'2010-03-06 06:33:36', u'log': 66, u'process': 1, u'ended_ts': None, u'state': u'PENDING', u'started_ts': None}}] <type 'list'>

有什么想法吗?

最佳答案

看起来返回的是 JSON 字符串的 repr(),而不是 JSON 字符串本身。因此,服务器上出现了一些问题。

关于python json 加载和 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2392501/

相关文章:

python - 在另一个容器内使用Docker容器的数据

python - 每次运行 H2OXGBoostEstimator 时出错

python - 为什么我的元组值会改变 (Python)?

javascript - 属性名称的 JSON 语法

javascript - 如何从 JSON 数组中检索 JSON 数据?

python - 使用 AutoCAD 中的 lisp 应用程序和 Python 3.5

javascript - 地下天气 API - Forcast 帮助 - Javascript

ios - 有没有办法检查字符串是否包含 Unicode 字母?

python - 在 python 3 和 2 中工作的 Unicode 文字

Python - 由于文件名中存在特殊字符而导致 "The system cannot find the file specified"