失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python json转字符串_在python中将json转换为字符串

python json转字符串_在python中将json转换为字符串

时间:2019-08-07 22:09:21

相关推荐

python json转字符串_在python中将json转换为字符串

一开始我没有清楚地解释我的问题。在python中将json转换为字符串时,请尝试使用str()和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"\'}'

我的预期输出: "{'jsonKey': 'jsonValue','title': 'hello world\"'}"

对我来说,不必再次将输出字符串更改为json(dict)。

这该怎么做?

如果觉得《python json转字符串_在python中将json转换为字符串》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。