0
点赞
收藏
分享

微信扫一扫

jquery.parseJSON


 

jQuery.parseJSON

 

      ------   接受一个标准的JSON字符串,返回解析后的对象

 

1.4.1版本开始引入!

 

     1、参数情况:

 

         --- 如果是下列情况,均返回null

 

  • null
  • undefined
  • 空(不传)
  • 空字符串

    源码来自1.4.1版本:

 


parseJSON:function(data){
       //验证
       if(typeof data !== "string" || !data){
             return null;
       } 

       // Make sure the incoming data is actual JSON
       // Logic borrowed from http://json.org/json2.js
       if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
			.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
			.replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {

			// Try to use the native JSON parser first
			return window.JSON && window.JSON.parse ?
				window.JSON.parse( data ) :
				(new Function("return " + data))();

         } else {
			jQuery.error( "Invalid JSON: " + data );
	 }


}


 

 

 

 

 

  扩展阅读:   http://api.jquery.com/jQuery.parseJSON/

 

 

举报

相关推荐

0 条评论