parseJSON: function(data) {
//如果参数为空或参数类型不是string,则返回null
if ( !data || typeof data !== "string") {
return null;
}
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim( data );//去掉字符串首尾空格
// Attempt to parse using the native JSON parser first
//使用浏览器提供的JSON解析函数来解析JSON字符串
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
// Make sure the incoming data is actual JSON
/*使用正则表达式判断JSON字符串格式是否正确
rvalidchars = /^[\],:{}\s]*$/,
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g,
rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-? (?:\d\d*\.|)\d+(?:[eE][\-+]?\d+|)/g
*/
if ( rvalidchars.test( data.replace( rvalidescape, "@" )
.replace( rvalidtokens, "]" )
.replace( rvalidbraces, "")) ) {
//使用原生的js Function()方法解析json字符串
return ( new Function( "return " + data ) )();
}
jQuery.error( "Invalid JSON: " + data );
}