Python json.loads 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14771570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:23:52  来源:igfitidea点击:

Python json.loads doesn't work

pythonjson

提问by vinni_pux

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)

采纳答案by Martijn Pieters

Your JSON data is enclosed in extra quotes making it a JSON string, andthe data contained within that string is notJSON.

您的 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 jsonmodule is concerned, decDatacould just as well have contained "This is not JSON"and postDatawould 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.

修复产生这个字符串的任何东西,你的观点很好,是输入被破坏了。

回答by Sasha Zezulinsky

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

要解决错误“期望用双引号括起来的属性名称”,您可以执行以下操作:

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

回答by Manoj Jadhav

You can also go with eval:

你也可以去eval

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

It works and returns you a dict.

它可以工作并返回一个字典。