我试图显示双引号,但它显示了一个反斜杠:

"maingame": {
    "day1": {
        "text1": "Tag 1",
        "text2": "Heute startet unsere Rundreise \\\"Example text\\\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> "
    }
}

当在html中呈现时,它显示为“示例文本\”。正确的方法是什么?


当前回答

试试这个:

"maingame": {
  "day1": {
    "text1": "Tag 1",
     "text2": "Heute startet unsere Rundreise \" Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> "
  }
}

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

其他回答

试试这个:

"maingame": {
  "day1": {
    "text1": "Tag 1",
     "text2": "Heute startet unsere Rundreise \" Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> "
  }
}

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

对于那些想使用开发人员powershell。下面是要添加到settings.json中的行:

"terminal.integrated.automationShell.windows": "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe",
"terminal.integrated.shellArgs.windows": [
    "-noe",
    "-c",
    " &{Import-Module 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\Common7\\Tools\\Microsoft.VisualStudio.DevShell.dll'; Enter-VsDevShell b7c50c8d} ",
    ],

它显示了反斜杠因为你也在转义反斜杠。

除了双引号,如果想在JSON带引号字符串中包含反斜杠,还必须转义反斜杠。然而,如果你打算在转义序列中使用反斜杠,显然你不应该转义它。

何时何地用“\\\”代替。好吧,如果你和我一样,当我发现这个帖子后意识到我在做什么时,你会觉得自己和我一样愚蠢。

如果你正在制作一个.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.

<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:由于它是一个JavaScript文本字符串,双反斜杠双引号\\"也可以;因为双引号不需要在单引号字符串中转义,例如'\"'和'"'会产生相同的JS字符串。

注意,这通常发生在内容被“双重编码”的情况下,这意味着编码算法被意外地调用了两次。

第一个调用将对"text2"值进行编码:

今天我们将以" examp文字"为起点每天新的目的地会向我们驶来

今天我们的导游工作\每天新的目的地会向我们驶来

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

今天我们的导游工作\每天新的目的地会向我们驶来

每天我们的实地旅行\\\每天新的目的地会向我们驶来

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