Python json。负担不起作用

时间:2022-06-21 07:05:32

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

我一直试图弄明白如何在Python中加载JSON对象。

   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

Where am I going wrong?

我哪里做错了?

Error Code (JScript):

错误代码(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);

True Code (JScript):

真正的代码(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));

True Code (Python):

真正的代码(Python):

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

3 个解决方案

#1


8  

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

您的JSON数据包含在额外的引号中,使其成为JSON字符串,并且该字符串中包含的数据不是JSON。

Print repr(decData) instead, you'll get:

打印repr(decData),你会得到:

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

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库正确地将其解释为带有文字内容{'name': 'journal2'}的一个字符串。如果去掉了外部引号,所包含的字符是无效的JSON,因为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'.

对于所有的json模块,decData也可以包含“This is not json”,postData将被设置为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.

修正任何产生这个字符串的东西,你的视图是好的,它是被破坏的输入。

#2


2  

To workaround the error 'Expecting property name enclosed in double quotes' you can do:

要解决“期望属性名包含在双引号中”的错误,您可以这样做:

import json
data = "{'name' : 'journal2'}"
json.loads(json.dumps(data))

#3


-2  

You can also go with eval:

你也可以使用eval:

data = "{'name' : 'test'}"
eval(data)

It works and returns you a dict.

它会工作并返回一个命令给你。

#1


8  

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

您的JSON数据包含在额外的引号中,使其成为JSON字符串,并且该字符串中包含的数据不是JSON。

Print repr(decData) instead, you'll get:

打印repr(decData),你会得到:

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

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库正确地将其解释为带有文字内容{'name': 'journal2'}的一个字符串。如果去掉了外部引号,所包含的字符是无效的JSON,因为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'.

对于所有的json模块,decData也可以包含“This is not json”,postData将被设置为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.

修正任何产生这个字符串的东西,你的视图是好的,它是被破坏的输入。

#2


2  

To workaround the error 'Expecting property name enclosed in double quotes' you can do:

要解决“期望属性名包含在双引号中”的错误,您可以这样做:

import json
data = "{'name' : 'journal2'}"
json.loads(json.dumps(data))

#3


-2  

You can also go with eval:

你也可以使用eval:

data = "{'name' : 'test'}"
eval(data)

It works and returns you a dict.

它会工作并返回一个命令给你。