使用Python解析JSON并删除'“和/

时间:2017-03-12 07:57:37

标签: python json

我正在使用Python从MySQL导入数据并将数据作为JSON返回到Google地图以绘制点并添加描述。

但是,我注意到,如果用户输入此点的数据

“这太棒了,真是太棒了!”

我最终得到了看起来像这样的JSON

[{"description" : "This is really amazing it/'s so cool! }]

我的地图不喜欢。我想知道是否有人可以解释如何从JSON中删除特殊字符,因此如果用户输入上面的行,它将返回为

[{"description" : "This is really amazing its so cool! }]

没有单引号,双引号,斜线等,一切都很好。我试过手动删除特殊章程,一切都完美无瑕!

1 个答案:

答案 0 :(得分:1)

您的双引号将以斜杠进行转义。

payload = {'description': '"This is really amazing its so cool!"'}
json_str = json.dumps(payload)
# json auto add a slash to escape double quote
print(json_str) # => {"description": "\"This is really amazing its so cool!\""}

# Extract the json string will auto remove the slash
# You don't need to handle them manually
extracted_payload = json.loads(json_str)
print(extracted_payload['discription']) # => '"This is really amazing its so cool!"'