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

python json转字符串_在python中将json转换为字符串一开始我没有清楚地解释我的问题 在 python 中将 json 转换为字符串时 请尝试使用 str 和 json dumps data jsonKey jsonValue title hello world print json dumps data jsonKey jsonValue title hello world


一开始我没有清楚地解释我的问题。在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)。

这该怎么做?

编程小号
上一篇 2025-02-27 19:27
下一篇 2025-03-05 23:21

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/82479.html