Single vs double quotes in JSON

2019-01-01 11:08发布

My code:

import simplejson as json

s = "{'username':'dfdsfdsf'}" #1
#s = '{"username":"dfdsfdsf"}' #2
j = json.loads(s)

#1 definition is wrong

#2 definition is right

I was heard that in Python that single double quote can be interchangable, can anyone explain this for me?

标签: python json
8条回答
刘海飞了
2楼-- · 2019-01-01 11:57
import json
data = json.dumps(list)
print(data)

The above code snippet should work.

查看更多
一个人的天荒地老
3楼-- · 2019-01-01 12:00

you can use ast.literal_eval()

>>> import ast
>>> s = "{'username':'dfdsfdsf'}"
>>> ast.literal_eval(s)
{'username': 'dfdsfdsf'}
查看更多
登录 后发表回答