现在的位置: 首页 > 综合 > 正文

jQuery方法扩展:type, toJSON, evalJSON

2013年10月31日 ⁄ 综合 ⁄ 共 2112字 ⁄ 字号 评论关闭

 

jQuery居然都没有JSON的decode和encode,精确类型判断也没有,囧……自己动手写吧!不过这些东西在网上都已经有很好的版本了,自己也不用太费脑筋,拿来用吧!类型判断在这里有一段很好的代码:http://lucassmith.name/pub/typeof.html,JSON的decode和encode就直接用Mootools的吧!(不过Mootools里面的JSON.encode方法还不够完美,我作了一些完善。)

  1. /** 
  2. * extension of JSON, type for jQuery 
  3. * AUTHOR: xushengs@gmail.com 
  4. * LICENSE: http://www.opensource.org/licenses/mit-license.php 
  5. * WEBSITE: http://fdream.net/ 
  6. */ 
  7. (function($) { 
  8.     // the code of this function is from 
  9.     // http://lucassmith.name/pub/typeof.html 
  10.     $.type = function(o) { 
  11.         var _toS = Object.prototype.toString; 
  12.         var _types = { 
  13.             'undefined': 'undefined', 
  14.             'number': 'number', 
  15.             'boolean': 'boolean', 
  16.             'string': 'string', 
  17.             '[object Function]': 'function', 
  18.             '[object RegExp]': 'regexp', 
  19.             '[object Array]': 'array', 
  20.             '[object Date]': 'date', 
  21.             '[object Error]': 'error' 
  22.         }; 
  23.  
  24.         return _types[typeof o] || _types[_toS.call(o)] || (o ? 'object' : 'null'); 
  25.     }; 
  26.  
  27.     // the code of these two functions is from mootools 
  28.     // http://mootools.net 
  29.     var $specialChars = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' }; 
  30.     var $replaceChars = function(chr) { 
  31.         return $specialChars[chr] || '\\u00' + Math.floor(chr.charCodeAt() / 16).toString(16) + (chr.charCodeAt() % 16).toString(16); 
  32.     }; 
  33.  
  34.     $.toJSON = function(o) { 
  35.         var s = []; 
  36.         switch ($.type(o)) { 
  37.             case 'undefined': 
  38.                 return 'undefined'; 
  39.                 break; 
  40.             case 'null': 
  41.                 return 'null'; 
  42.                 break; 
  43.             case 'number': 
  44.             case 'boolean': 
  45.             case 'date': 
  46.             case 'function': 
  47.                 return o.toString(); 
  48.                 break; 
  49.             case 'string': 
  50.                 return '"' + o.replace(/[\x00-\x1f\\"]/g, $replaceChars) + '"'; 
  51.                 break; 
  52.             case 'array': 
  53.                 for (var i = 0, l = o.length; i < l; i++) { 
  54.                     s.push($.toJSON(o[i])); 
  55.                 } 
  56.                 return '[' + s.join(',') + ']'; 
  57.                 break; 
  58.             case 'error': 
  59.             case 'object': 
  60.                 for (var p in o) { 
  61.                     s.push(p + ':' + $.toJSON(o[p])); 
  62.                 } 
  63.                 return '{' + s.join(',') + '}'; 
  64.                 break; 
  65.             default: 
  66.                 return ''; 
  67.                 break; 
  68.         } 
  69.     }; 
  70.  
  71.     $.evalJSON = function(s) { 
  72.         if ($.type(s) != 'string' || !s.length) return null; 
  73.         return eval('(' + s + ')'); 
  74.     }; 
  75. })(jQuery);

抱歉!评论已关闭.