Javascript JSON 中的 JSON 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26238796/
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
JSON String inside a JSON
提问by moustacheman
I want to create a JSON string inside a JSON request. Here is my code,
我想在 JSON 请求中创建一个 JSON 字符串。这是我的代码,
JS
JS
var x = {
a: 1,
b: 'a sample text',
};
var request = {
t: JSON.stringify(x),
c: 2,
r: 'some text'
};
console.log(request);
Can someone help me how to escape the double quotes?
有人可以帮助我如何转义双引号吗?
Console
安慰
Object {
t: "{"a":1,"b":"a sample text"}", //This creates a problem, double quotes inside double quotes.
c: 2,
r: "some text"
}
Thanks in advance.
提前致谢。
采纳答案by MrCode
That's just the way the browser console shows you the value of a string, by wrapping in double quotes for the output. This is perfectly normal and nothing is broken.
这就是浏览器控制台通过将输出括在双引号中来向您显示字符串值的方式。这是完全正常的,没有任何损坏。
You can test it by transforming your JSON string back to an object and using a property.
您可以通过将 JSON 字符串转换回对象并使用属性来测试它。
console.log( JSON.parse(request.t).b ); // a sample text
回答by Bergi
There is no problem. It's just your console.logthat shows all strings by simply delimiting with ".
没有问题。只是您console.log通过简单地用 分隔来显示所有字符串"。
As you say this requestobject is used in a JSON request, where it will be JSON.stringifyed another time, with the valid result
如您所说,此request对象用于 JSON 请求,它将在JSON.stringify另一次使用时使用有效结果
{"t":"{\"a\":1,\"b\":\"a sample text\"}","c":2,"r":"some text"}

