简体   繁体   English

jQuery JSON.parse异常

[英]jQuery JSON.parse exception

This will fail: 这将失败:

 var obj= JSON.parse('{"a": "\"aa\" vv","b": "b"}');

-> Uncaught SyntaxError: Unexpected token a in JSON at position 8 Whats the issue here? -> 未捕获到的SyntaxError:位置8中的JSON中意外的令牌a在这里是什么问题? White space beteween a and v? a和v之间有空格? documentations say that this should be working fine? 文档说这应该工作正常吗?

thx 谢谢

You need to use \\\\ instead of \\ 您需要使用\\\\代替\\

 var obj= JSON.parse('{"a": "\\\\"aa\\\\" vv","b": "b"}'); console.log(obj.a); console.log(obj.b); 

You should use double back slash \\\\ : 您应该使用双反斜杠\\\\

var obj= JSON.parse('{"a": "\\"aa\\" vv","b": "b"}');
console.log(obj);

Here is explaination of difference between \\ and \\\\ usage between simpleObject and object in form of String as argument of JSON.parse() 这是对SimpleObject和对象之间\\\\\\用法之间的区别的解释,以String形式作为JSON.parse()参数

 var realObj= {"a": "\\"aa\\" vv","b": "b"}; // doubleQuote is escaped with single \\ so that doubleQuote becomes part of value string in key-value pair. Hence, single backwardSlash console.log("realObj.a==>"+realObj.a); console.log("realObj.b==>"+realObj.b); var objFromParsedJSON= JSON.parse('{"a": "\\\\"aa\\\\" vv","b": "b"}'); //doubleQuote is escaped with double \\\\ so that the argument of JSON.parse should also escape doubleQuote and interpret it as \\" after processing. Hence, double \\\\ console.log("objFromParsedJSON.a==>"+objFromParsedJSON.a); console.log("objFromParsedJSON.b==>"+objFromParsedJSON.b); 

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

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