我想在 JSON 请求中创建一个 JSON 字符串。这是我的代码,
JS
var x = {
a: 1,
b: 'a sample text',
};
var request = {
t: JSON.stringify(x),
c: 2,
r: 'some text'
};
console.log(request);
谁能帮我转义双引号?
控制台
Object {
t: "{"a":1,"b":"a sample text"}", //This creates a problem, double quotes inside double quotes.
c: 2,
r: "some text"
}
提前致谢。
最佳答案
没有问题。这只是您的 console.log
,它通过简单地用 "
分隔来显示所有字符串。
正如您所说,此 request
对象在 JSON 请求中使用,它将在另一次 JSON.stringify
ed 中得到有效结果
{"t":"{\"a\":1,\"b\":\"a sample text\"}","c":2,"r":"some text"}
关于javascript - JSON 中的 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26238796/