我是靠谱客的博主 机智小霸王,这篇文章主要介绍python json转字符串_在python中将json转换为字符串,现在分享给大家,希望可以做个参考。

一开始我没有清楚地解释我的问题。在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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部