python json 加载和 unicode

发布于 2024-08-24 12:00:12 字数 957 浏览 3 评论 0原文

在以下情况下,我得到了 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'>

有什么想法吗?

I have the following case where I get the result of UTF-8 encoded HTTP response. I want to load the response content(JSON). However I don't know why I have to do 2 json.loads so that I get the final list:

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'>

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光下的微笑 2024-08-31 12:00:12

看起来返回的是 JSON 字符串的 repr(),而不是 JSON 字符串本身。所以,服务器上有东西坏了。

It looks like the repr() of the JSON string is what's being returned instead of the JSON string itself. So, something is broken on the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文