如何避免JSON中的双引号?

时间:2021-09-18 06:13:07

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中呈现时,它显示为\“示例文本\”。正确的方法是什么?

6 个解决方案

#1


312  

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

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

#2


16  

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引用的字符串中包含一个,还必须避免反斜杠。但是,如果您打算在转义序列中使用反斜杠,显然您不应该逃避它。

#3


12  

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编辑器”,在JSON.parse(文本)的输出中有双引号,那么你要找的是三重反斜杠双引号\\\。这是因为你正在构建HTML中的文本字符串 <脚本> 块,第一个双反斜杠将一个反斜杠插入到字符串变量然后下面的反斜杠双引号将双引号插入字符串,这样生成的脚本字符串包含\”标准答案和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文本字符串,双反斜杠双引号也可以工作;因为双引号不需要在一个引用的字符串(例如'\ '和' ')中转义。

#4


6  

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.

FROM: Heute startet unsere Rundreise“示例文本”。在这里我们可以看到。

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

TO: Heute startet unsere Rundreise \“示例文本\”。在这里我们可以看到。

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 \“示例文本\”。在这里我们可以看到。

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

TO: Heute startet unsere Rundreise \\\“示例文本\\”。在这里我们可以看到。

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.

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

#5


1  

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,'\\');
};

#6


1  

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"}

#1


312  

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

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

#2


16  

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引用的字符串中包含一个,还必须避免反斜杠。但是,如果您打算在转义序列中使用反斜杠,显然您不应该逃避它。

#3


12  

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编辑器”,在JSON.parse(文本)的输出中有双引号,那么你要找的是三重反斜杠双引号\\\。这是因为你正在构建HTML中的文本字符串 <脚本> 块,第一个双反斜杠将一个反斜杠插入到字符串变量然后下面的反斜杠双引号将双引号插入字符串,这样生成的脚本字符串包含\”标准答案和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文本字符串,双反斜杠双引号也可以工作;因为双引号不需要在一个引用的字符串(例如'\ '和' ')中转义。

#4


6  

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.

FROM: Heute startet unsere Rundreise“示例文本”。在这里我们可以看到。

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

TO: Heute startet unsere Rundreise \“示例文本\”。在这里我们可以看到。

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 \“示例文本\”。在这里我们可以看到。

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

TO: Heute startet unsere Rundreise \\\“示例文本\\”。在这里我们可以看到。

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.

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

#5


1  

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,'\\');
};

#6


1  

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"}