JSON中如何转义字符串中的双引号(转载)

时间:2023-03-10 07:20:19
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?