如何在 JSON 中转义双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15637429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:49:48  来源:igfitidea点击:

How to escape double quotes in JSON

json

提问by user1937021

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?

在 html 中呈现时,它显示为\"Example text\". 什么是正确的方法?

回答by kamituel

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).

\引号前只有一个反斜杠 ( ))。

回答by Gregor y

When and where to use \\\"instead. OK if you are like me you will feel just as silly as I did when I realized what I was doing after I found this thread.

何时何地使用\\\"。好吧,如果你和我一样,当我发现这个线程后,当我意识到自己在做什么时,你会觉得自己很傻。

If you're making a .json text file/stream and importing the data from there then the main stream answer of just one backslash before the double quotes:\"is the one you're looking for.

如果您正在制作 .json 文本文件/流并从那里导入数据,那么双引号前只有一个反斜杠的主流答案:\"就是您正在寻找的。

However if you're like me and you're trying to get the w3schools.com "Tryit Editor" to have a double quotes in the output of the JSON.parse(text), then the one you're looking for is the triple backslash double quotes \\\". This is because you're building your text string within an HTML <script>block, and the first double backslash inserts a single backslash into the string variable then the following backslash double quote inserts the double quote into the string so that the resulting script string contains the \"from the standard answer and the JSON parser will parse this as just the double quotes.

但是,如果您像我一样,并且试图让 w3schools.com “Tryit Editor”在 JSON.parse(text) 的输出中有一个双引号,那么您要查找的就是三元组反斜杠双引号\\\"。这是因为您在 HTML<script>块中构建文本字符串,并且第一个双反斜杠将单个反斜杠插入字符串变量,然后下面的反斜杠双引号将双引号插入字符串中,以便生成的脚本字符串包含\"from标准答案和 JSON 解析器会将其解析为双引号。

<script>
  var text="{";
  text += '"quip":"\\"If nobody is listening, then you\'re likely talking to the wrong audience.\\""';
  text += "}";
  var obj=JSON.parse(text);
</script>

+1: since it's a JavaScript text string, a double backslash double quote \\"would work too; because the double quote does not need escaped within a single quoted string eg '\"'and '"'result in the same JS string.

+1:因为它是一个 JavaScript 文本字符串,所以双反斜杠双引号\\"也可以;因为双引号不需要在单引号字符串中转义,例如'\"''"'导致相同的 JS 字符串。

回答by Alex Worden

It's showing the backslash because you're also escaping the backslash.

它显示反斜杠,因为您也在逃避反斜杠。

Aside from double quotes, you must also escape backslashes if you want to include one in your JSON quoted string. However if you intend to use a backslash in an escape sequence, obviously you shouldn't escape it.

除了双引号,如果你想在你的 JSON 引用字符串中包含一个,你还必须转义反斜杠。但是,如果您打算在转义序列中使用反斜杠,显然您不应该对其进行转义。

回答by Ben

Note that this most often occurs when the content has been "double encoded", meaning the encoding algorithm has accidentally been called twice.

请注意,这最常发生在内容被“双重编码”时,这意味着编码算法被意外调用了两次。

The first call would encode the "text2" value:

第一次调用将对“text2”值进行编码:

FROM:Heute startet unsere Rundreise "Example text". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.

来自:Heute startet unsere Rundreise“示例文本”。Jeden Tag wird ein neues Reiseziel angesteuert bis wir。

TO:Heute startet unsere Rundreise \"Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.

TO:Heute startet unsere Rundreise \"示例文本\"。Jeden Tag wird ein neues Reiseziel angesteuert bis wir。

A second encoding then converts it again, escaping the already escaped characters:

第二个编码然后再次转换它,转义已经转义的字符:

FROM:Heute startet unsere Rundreise \"Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.

来自:Heute startet unsere Rundreise \"Example text\"。Jeden Tag wird ein neues Reiseziel angesteuert bis wir。

TO:Heute startet unsere Rundreise \\\"Example text\\\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.

TO:Heute startet unsere Rundreise \\\"示例文本\\\"。Jeden Tag wird ein neues Reiseziel angesteuert bis wir。

So, if you are responsible for the implementation of the server here, check to make sure there aren't two steps trying to encode the same content.

因此,如果您负责此处服务器的实现,请检查以确保没有两个步骤试图对相同的内容进行编码。

回答by manas

if you want to escape double quote in JSON use \\ to escape it.

如果你想在 JSON 中转义双引号,请使用 \\ 来转义它。

example if you want to create json of following javascript object

例如,如果您想创建以下 javascript 对象的 json

{time: '7 "o" clock'}

then you must write in following way

那么你必须按照以下方式写

'{"time":"7 \"o\" clock"}'

if we parse it using JSON.parse()

如果我们解析它使用 JSON.parse()

JSON.parse('{"time":"7 \"o\" clock"}')

result will be

结果将是

{time: "7 "o" clock"}

回答by mbokil

To escape backslashes that cause problems for JSON data I use this function.

为了避免导致 JSON 数据出现问题的反斜杠,我使用了这个函数。

//escape backslash to avoid errors
var escapeJSON = function(str) {
    return str.replace(/\/g,'\');
};