我是靠谱客的博主 机智小霸王,最近开发中收集的这篇文章主要介绍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 json转字符串_在python中将json转换为字符串所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部