Python json.loads不起作用 [英] Python json.loads doesn't work

查看:522
本文介绍了Python json.loads不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚如何在Python中加载JSON对象.

I've been trying to figure out how to load JSON objects in Python.

   def do_POST(self):
    length = int(self.headers['Content-Length'])
    decData = str(self.rfile.read(length))
    print decData, type(decData)
    "{'name' : 'journal2'}" <type 'str'>
    postData = json.loads(decData)
    print postData, type(postData)
    #{'name' : 'journal2'} <type 'unicode'>
    postData = json.loads(postData)
    print postData, type(postData)
    # Error: Expecting property name enclosed in double quotes

我要去哪里错了?

错误代码(JScript):

var data = "{'name':'journal2'}";
var http_request = new XMLHttpRequest();
http_request.open( "post", url, true );
http_request.setRequestHeader('Content-Type', 'application/json');
http_request.send(data);

真实代码(JScript):

var data = '{"name":"journal2"}';
var http_request = new XMLHttpRequest();
http_request.open( "post", url, true );
http_request.setRequestHeader('Content-Type', 'application/json');
http_request.send(JSON.stringify(data));

真实代码(Python):

   def do_POST(self):
    length = int(self.headers['Content-Length'])
    decData = self.rfile.read(length)
    postData = json.loads(decData)
    postData = json.loads(postData)

推荐答案

您的JSON数据用引号引起来,使其成为JSON字符串, 该字符串中包含的数据不是 JSON.

Your JSON data is enclosed in extra quotes making it a JSON string, and the data contained within that string is not JSON.

改为打印repr(decData),您将获得:

'"{\'name\' : \'journal2\'}"'

,并且JSON库正确地将其解释为具有文字内容{'name' : 'journal2'}的一个字符串.如果您除去外部引号,则包含的字符不是有效的JSON,因为JSON字符串必须始终用双引号引起来.

and the JSON library is correctly interpreting that as one string with the literal contents {'name' : 'journal2'}. If you stripped the outer quotes, the contained characters are not valid JSON, because JSON strings must always be enclosed in double quotes.

对于所有json模块,decData可能还包含"This is not JSON",而postData也将设置为u'This is not JSON'.

For all the json module is concerned, decData could just as well have contained "This is not JSON" and postData would have been set to u'This is not JSON'.

>>> import json
>>> decData = '''"{'name' : 'journal2'}"'''
>>> json.loads(decData)
u"{'name' : 'journal2'}"
>>> json.loads(json.loads(decData))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 1 (char 1)

修复生成该字符串的任何内容,您的视图很好,这是输入中断了.

Fix whatever is producing this string, your view is fine, it's the input that is broken.

这篇关于Python json.loads不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆