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?
I recently came up against a very similar problem, and believe my solution would work for you too. I had a text file which contained a list of items in the form:
I wanted to parse the above into a python list but was not keen on eval() as I couldn't trust the input. I tried first using JSON but it only accepts double quoted items, so I wrote my own very simple lexer for this specific case (just plug in your own "stringtoparse" and you will get as output list: 'items')
Hopefully it is useful to somebody. Enjoy!
Works for me
You can dump JSON with double quote by:
JSON syntax is not Python syntax. JSON requires double quotes for its strings.
demjson is also a good package to solve the problem of bad json syntax:
Usage:
Edit:
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.
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
This might otherwise break if it used single quotes, too.