I did not explain my questions clearly at beginning.
Try to use str()
and json.dumps()
when converting JSON to string in python.
>>> data = {'jsonKey': 'jsonValue',"title": "hello world"}
>>> print json.dumps(data)
{"jsonKey": "jsonValue", "title": "hello world"}
>>> print str(data)
{'jsonKey': 'jsonValue', 'title': 'hello world'}
>>> json.dumps(data)
'{"jsonKey": "jsonValue", "title": "hello world"}'
>>> str(data)
"{'jsonKey': 'jsonValue', 'title': 'hello world'}"
My question is:
>>> data = {'jsonKey': 'jsonValue',"title": "hello world'"}
>>> str(data)
'{'jsonKey': 'jsonValue', 'title': "hello world'"}'
>>> json.dumps(data)
'{"jsonKey": "jsonValue", "title": "hello world'"}'
>>>
My expected output: "{'jsonKey': 'jsonValue','title': 'hello world''}"
>>> data = {'jsonKey': 'jsonValue',"title": "hello world""}
File "<stdin>", line 1
data = {'jsonKey': 'jsonValue',"title": "hello world""}
^
SyntaxError: EOL while scanning string literal
>>> data = {'jsonKey': 'jsonValue',"title": "hello world""}
>>> json.dumps(data)
'{"jsonKey": "jsonValue", "title": "hello world\""}'
>>> str(data)
'{'jsonKey': 'jsonValue', 'title': 'hello world"'}'
My expected output: "{'jsonKey': 'jsonValue','title': 'hello world"'}"
It is not necessary to change the output string to json (dict) again for me.
How to do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…