日期:2014-05-16  浏览次数:20408 次

关于json的parse个人最近参考写的一个完整版本

?

/*
*isString-judge the source is or not string*
*@function*
*@param source*
*@return {boolean}*
*/
ZYC.lang.isString = function(source){
    return Object.prototype.toString.call(source) === "[object String]";
};

/*
*parse-parse the string to json object*
*@function*
*@param {string} source*
*@return {Object}json object*
*/
ZYC.json.parse = function(source){
   //there are three ways to do it
   //we choose the old way
   if(ZYC.lang.isString(source)){
      //try the native JSON parser first
	  if(window.JSON &&  window.JSON.parse){
	      return window.JSON.parse(source);
	  }else{
	      //there are most three ways to do it
		  //just like the old eval('(' +source+')')
	      return (new Function("return" + source))();
	  }
   }
   return null;
};