概述
一开始我没有清楚地解释我的问题。在python中将json转换为字符串时,尝试使用"{'jsonKey': 'jsonValue','title': 'hello world"'}"和json.dumps()。
>>> 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'}"
我的问题是:
>>> data = {'jsonKey': 'jsonValue',"title": "hello world'"}
>>> str(data)
'{'jsonKey': 'jsonValue', 'title': "hello world'"}'
>>> json.dumps(data)
'{"jsonKey": "jsonValue", "title": "hello world'"}'
>>>
我的预期输出:"{'jsonKey': 'jsonValue','title': 'hello world"'}"
>>> data = {'jsonKey': 'jsonValue',"title": "hello world""}
File "", 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"'}"
对我而言,不必再次将输出字符串更改为json(dict)。
这该怎么做?
最后
以上就是机智小霸王为你收集整理的python json转字符串_在python中将json转换为字符串的全部内容,希望文章能够帮你解决python json转字符串_在python中将json转换为字符串所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复