My code:
我的代码:
import simplejson as json
s = "{'username':'dfdsfdsf'}" #1
#s = '{"username":"dfdsfdsf"}' #2
j = json.loads(s)
#1
definition is wrong
# 1的定义是错误的
#2
definition is right
# 2的定义是正确的
I was heard that in Python that single double quote can be interchangable, can anyone explain this for me?
我听说在Python中单双引号是可以互换的,有人能给我解释一下吗?
6 个解决方案
#1
113
JSON syntax is not Python syntax. JSON requires double quotes for its strings.
JSON语法不是Python语法。JSON的字符串需要双引号。
#2
72
you can use ast.literal_eval()
您可以使用ast.literal_eval()
>>> import ast
>>> s = "{'username':'dfdsfdsf'}"
>>> ast.literal_eval(s)
{'username': 'dfdsfdsf'}
#3
26
You can dump JSON with double quote by:
可以将JSON转储为双引号:
from json import dumps
#mixing single and double quotes
data = {'jsonKey': 'jsonValue',"title": "hello world"}
jsonString = json.dumps(data)
#get string with all double quotes
#4
7
demjson is also a good package to solve the problem of bad json syntax:
demjson也是一个很好的解决不好json语法问题的软件包:
pip install demjson
Usage:
用法:
from demjson import decode
bad_json = "{'username':'dfdsfdsf'}"
python_dict = decode(bad_json)
Edit:
编辑:
demjson.decode
is a great tool for damaged json, but when you are dealing with big amourt of json dataast.literal_eval
is a better match and much faster.decode是一个很好的针对损坏的json的工具,但是当你处理大量的json数据时,literal_eval是一个更好的匹配,而且速度更快。
#5
0
As said, JSON is not Python syntax. You need to use double quotes in JSON. Its creator is (in-)famous for using strict subsets of allowable syntax to ease programmer cognitive overload.
如前所述,JSON不是Python语法。您需要在JSON中使用双引号。它的创建者以使用严格的允许语法子集来缓解程序员的认知过载而闻名。
It is really useful to know that there are no single quotes in a JSON string. Say, you copied and pasted it from a browser console/whatever. Then, you can just type
知道JSON字符串中没有单引号是非常有用的。比方说,你复制并粘贴到浏览器控制台/随便什么。然后,你可以直接输入
a = json.loads('very_long_json_string_pasted_here')
This might otherwise break if it used single quotes, too.
如果使用单引号,则可能会中断。
#6
-3
import json
data = json.dumps(list)
print(data)
The above code snippet should work.
上面的代码片段应该可以工作。
#1
113
JSON syntax is not Python syntax. JSON requires double quotes for its strings.
JSON语法不是Python语法。JSON的字符串需要双引号。
#2
72
you can use ast.literal_eval()
您可以使用ast.literal_eval()
>>> import ast
>>> s = "{'username':'dfdsfdsf'}"
>>> ast.literal_eval(s)
{'username': 'dfdsfdsf'}
#3
26
You can dump JSON with double quote by:
可以将JSON转储为双引号:
from json import dumps
#mixing single and double quotes
data = {'jsonKey': 'jsonValue',"title": "hello world"}
jsonString = json.dumps(data)
#get string with all double quotes
#4
7
demjson is also a good package to solve the problem of bad json syntax:
demjson也是一个很好的解决不好json语法问题的软件包:
pip install demjson
Usage:
用法:
from demjson import decode
bad_json = "{'username':'dfdsfdsf'}"
python_dict = decode(bad_json)
Edit:
编辑:
demjson.decode
is a great tool for damaged json, but when you are dealing with big amourt of json dataast.literal_eval
is a better match and much faster.decode是一个很好的针对损坏的json的工具,但是当你处理大量的json数据时,literal_eval是一个更好的匹配,而且速度更快。
#5
0
As said, JSON is not Python syntax. You need to use double quotes in JSON. Its creator is (in-)famous for using strict subsets of allowable syntax to ease programmer cognitive overload.
如前所述,JSON不是Python语法。您需要在JSON中使用双引号。它的创建者以使用严格的允许语法子集来缓解程序员的认知过载而闻名。
It is really useful to know that there are no single quotes in a JSON string. Say, you copied and pasted it from a browser console/whatever. Then, you can just type
知道JSON字符串中没有单引号是非常有用的。比方说,你复制并粘贴到浏览器控制台/随便什么。然后,你可以直接输入
a = json.loads('very_long_json_string_pasted_here')
This might otherwise break if it used single quotes, too.
如果使用单引号,则可能会中断。
#6
-3
import json
data = json.dumps(list)
print(data)
The above code snippet should work.
上面的代码片段应该可以工作。