JS对象序列化为JSON对象

来源:互联网 发布:无线通信算法 招聘 编辑:程序博客网 时间:2024/06/05 05:07
Js代码  收藏代码
  1. /** 
  2.  * JSON 解析类 
  3.  * Copyright (c) 2010 YaoYiLang  
  4.  * @email  redrainyi@gmail.com 
  5.  * @datetime 2008-04-18 
  6.  * @version 1.0 
  7.  *  
  8.  * 方法: 
  9.  * 将JSON字符串解码为页面可识别的object对象 
  10.  * @param {String} json The JSON string 
  11.  * @return {Object} The resulting object  
  12.  * Object o = JSONUtil.decode(json); 
  13.  *  
  14.  * 将JS对象序列化为JSON字符串 
  15.  * @param {Mixed} o The variable to decode 
  16.  * @return {String} The JSON string 
  17.  * String json = JSONUtil.encode(o); 
  18.  */  
  19.   
  20. var JSONUtil;  
  21. if (!JSONUtil) {  
  22.     JSONUtil = {};  
  23. }  
  24. JSONUtil.decode = function(json) {  
  25.     try {  
  26.         return eval("\u0028" + json + '\u0029');  
  27.     } catch (exception) {  
  28.         return eval("\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064");  
  29.     }  
  30. };  
  31. JSONUtil.encode = (function() {  
  32.     var $ = !!{}.hasOwnProperty, _ = function($) {  
  33.         return $ < 10 ? "0" + $ : $  
  34.     }, A = {  
  35.         "\b" : "\\b",  
  36.         "\t" : "\\t",  
  37.         "\n" : "\\n",  
  38.         "\f" : "\\f",  
  39.         "\r" : "\\r",  
  40.         "\"" : "\\\"",  
  41.         "\\" : "\\\\"  
  42.     };  
  43.     return (function(C) {  
  44.         if (typeof C == "\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064" || C === null)  
  45.             return "null";  
  46.         else if (Object.prototype.toString.call(C) === "\u005b\u006f\u0062\u006a\u0065\u0063\u0074\u0020\u0041\u0072\u0072\u0061\u0079\u005d") {  
  47.             var B = ["\u005b"], G, E, D = C.length, F;  
  48.             for (E = 0; E < D; E += 1) {  
  49.                 F = C[E];  
  50.                 switch (typeof F) {  
  51.                     case "\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064" :  
  52.                     case "\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e" :  
  53.                     case "\u0075\u006e\u006b\u006e\u006f\u0077\u006e" :  
  54.                         break;  
  55.                     default :  
  56.                         if (G)  
  57.                             B.push("\u002c");  
  58.                         B.push(F === null ? "null" : this.encode(F));  
  59.                         G = true  
  60.                 }  
  61.             }  
  62.             B.push("\u005d");  
  63.             return B.join("")  
  64.         } else if ((Object.prototype.toString.call(C) === "\u005b\u006f\u0062\u006a\u0065\u0063\u0074\u0020\u0044\u0061\u0074\u0065\u005d"))  
  65.             return "\"" + C.getFullYear() + "-" + _(C.getMonth() + 1) + "-" + _(C.getDate()) + "T" + _(C.getHours()) + ":" + _(C.getMinutes()) + ":" + _(C.getSeconds()) + "\"";  
  66.         else if (typeof C == "\u0073\u0074\u0072\u0069\u006e\u0067") {  
  67.             if (/["\\\x00-\x1f]/.test(C))  
  68.                 return "\"" + C.replace(/([\x00-\x1f\\"])/g, function(B, _) {  
  69.                     var $ = A[_];  
  70.                     if ($)  
  71.                         return $;  
  72.                     $ = _.charCodeAt();  
  73.                     return "\\u00" + Math.floor($ / 16).toString(16) + ($ % 16).toString(16)  
  74.                 }) + "\"";  
  75.             return "\"" + C + "\""  
  76.         } else if (typeof C == "\u006e\u0075\u006d\u0062\u0065\u0072")  
  77.             return isFinite(C) ? String(C) : "null";  
  78.         else if (typeof C == "\u0062\u006f\u006f\u006c\u0065\u0061\u006e")  
  79.             return String(C);  
  80.         else {  
  81.             B = ["\u007b"], G, E, F;  
  82.             for (E in C)  
  83.                 if (!$ || C.hasOwnProperty(E)) {  
  84.                     F = C[E];  
  85.                     if (F === null)  
  86.                         continue;  
  87.                     switch (typeof F) {  
  88.                         case "\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064" :  
  89.                         case "\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e" :  
  90.                         case "\u0075\u006e\u006b\u006e\u006f\u0077\u006e" :  
  91.                             break;  
  92.                         default :  
  93.                             if (G)  
  94.                                 B.push("\u002c");  
  95.                             B.push(this.encode(E), "\u003a"this.encode(F));  
  96.                             G = true  
  97.                     }  
  98.                 }  
  99.             B.push("\u007d");  
  100.             return B.join("")  
  101.         }  
  102.     })  
  103. })();  
  104. window.JSONUtil = JSONUtil;  
0 0
原创粉丝点击