mou*_*man 11 javascript json escaping stringify
我想在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);
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我如何逃避双引号?
安慰
Object {
t: "{"a":1,"b":"a sample text"}", //This creates a problem, double quotes inside double quotes.
c: 2,
r: "some text"
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
There is no problem. It's just your console.log that shows all strings by simply delimiting with ".
As you say this request object is used in a JSON request, where it will be JSON.stringifyed another time, with the valid result
{"t":"{\"a\":1,\"b\":\"a sample text\"}","c":2,"r":"some text"}
Run Code Online (Sandbox Code Playgroud)
这就是浏览器控制台通过用输出的双引号括起来显示字符串值的方式.这是完全正常的,没有任何东西被打破.
您可以通过将JSON字符串转换回对象并使用属性来测试它.
console.log( JSON.parse(request.t).b ); // a sample text
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27736 次 |
| 最近记录: |