JSON中如何转义字符串中的双引号(转载)
问:
I'm trying to show double quotes but it shows one of the backslashes:
"maingame": { "day1": { "text1": "Tag 1", "text2": "Heute startet unsere Rundreise \\\"Example text\\\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> " } }
When rendering in the html it shows as \"Example text\". What is the correct way?
答:
Try this:
"maingame": { "day1": { "text1": "Tag 1", "text2": "Heute startet unsere Rundreise \" Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> " } }
(just one backslash (\) in front of quotes).
JSON字符串中的转义字符和C#一样,都是用反斜线"\"开头的,例如:
\b Backspace (ascii code 08) \f Form feed (ascii code 0C) \n New line \r Carriage return \t Tab \" Double quote \\ Backslash character
参考文献:
How to escape double quotes in JSON
How to escape special characters in building a JSON string?
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步