简体   繁体   English

如何在 JSON 字符串中转义双引号

[英]How to escape double quote in JSON string

I am trying to do parse a string Hello " test containing double quote which already escaped, but I get an error我正在尝试解析一个字符串Hello " test ,其中包含已经转义的双引号,但出现错误

JSON.parse(`{"x":"Hello \" test "}`)

Is there anything I missed here?我在这里错过了什么吗?

在此处输入图像描述

 JSON.parse(`{"x":"Hello \" test "}`)

You just need to escape the backslash \ , so it turns into two backslashes \\你只需要转义反斜杠\ ,所以它变成两个反斜杠\\

 console.log(JSON.parse('{"x":"Hello \\" test"}'))

 let mydata = `{"x":"Hello \" test "}` let escapeJsonFunc = function(str) { return str.replace(/\\/g,'\\'); }; console.log( escapeJsonFunc(mydata) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM