python - 使用 json.dumps(string) 转储字符串

标签 python json

我想将字符串放入 json.dumps()

string = "{data}"

print json.dumps(string)

然后我得到:

“{data}” 而不是:{data}

如何得到这个不带引号的字符串?

方法替换:

json.dumps(string.replace('"', '')

strip:

json.dumps(string.strip('"')

不起作用。

最佳答案

您不会转储 字符串,而是转储 字符串。

d = {"foo": "bar"}
j = json.dumps(d)
print(j)

输出:

{"foo": "bar"}

使用字符串作为 json.dumps 的参数是没有意义的它期望字典或列表/元组作为参数。

Serialize obj to a JSON formatted str using this conversion table.

关于python - 使用 json.dumps(string) 转储字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30187231/

相关文章:

java - 全世界的新闻api for android

regex - 从 Rails 3 中的 JSON 响应中剥离数字?

python - 从 Matplotlib 使用 ImageGrid 时如何调整子图的大小

python - 标准错误上的 Paramiko recv()/read()/readline(s)() 返回空字符串

python - Gensim doest_match 函数如何工作?

json 和 vb.net 10

json - 使用 SwifyJson 从 JSON 中获取数据

java - 迭代 Firebase 对象的 JSON 对象

python - 无法在 Keras 的 ImageDataGenerator 中将 preprocess_input 用作预处理函数

Python 测试 url 和图像类型