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

2024-06-01 14:00:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python从MySQL导入数据,并将数据作为JSON返回到Google地图,以绘制点并向点添加描述。你知道吗

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

“真是太棒了,太酷了!”你知道吗

我最终得到了如下的JSON

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

我的地图不喜欢。我想知道是否有人能解释如何从JSON中删除特殊字符,这样如果用户输入了上面的行,它将作为

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

没有单引号,双引号,斜杠等,一切都很好。我已经尝试手动删除特殊的章程和一切工作完美!你知道吗


Tags: 数据用户jsonsoisgooglemysql地图
1条回答
网友
1楼 · 发布于 2024-06-01 14:00:42

你的双引号将用斜杠转义。你知道吗

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!"'

相关问题 更多 >