`

json 转化

阅读更多

package com.ijo.demo;

public class Json {
 /**
  * JSON 解析类 方法: 将JSON字符串解码为页面可识别的object对象
  *
  * @param {String}
  *            json The JSON string
  * @return {Object} The resulting object Object o = JSON.decode(json);
  *
  * 将JS对象序列化为JSON对象
  * @param {Mixed}
  *            o The variable to decode
  * @return {String} The JSON string String json = JSON.encode(o);
  */ 
  
  
 var JSON;  
  
 if (!JSON) {  
     JSON = {};  
 }  
  
 /**
  * 将JSON字符串解码为页面可识别的object对象
  *
  * @param {String}
  *            json The JSON string
  * @return {Object} The resulting object
  */ 
 JSON.decode = function(json) {  
     return eval("(" + json + ')');  
 };  
 /**
  * 将JS对象序列化为JSON对象
  *
  * @param {Mixed}
  *            o The variable to encode
  * @return {String} The JSON string
  */ 
 JSON.encode = (function() {  
     var useHasOwn = !!{}.hasOwnProperty;  
     var pad = function(n) {  
         return n < ? " + n : n;  
     };  
     var m = {  
         "\b" : '\\b',  
         "\t" : '\\t',  
         "\n" : '\\n',  
         "\f" : '\\f',  
         "\r" : '\\r',  
         '"' : '\\"',  
         "\\" : '\\\\' 
     };  
     return (function(o) {  
         if (typeof o == "undefined" || o === null) {  
             return "null";  
         } else if (Object.prototype.toString.call(o) === '[object Array]') {  
             var a = ["["], b, i, l = o.length, v;  
             for (i =  i < l; i +=  {  
                 v = o[i];  
                 switch (typeof v) {  
                     case "undefined" :  
                     case "function" :  
                     case "unknown" :  
                         break;  
                     default :  
                         if (b) {  
                             a.push(',');  
                         }  
                         a.push(v === null ? "null" : JSON.encode(v));  
                         b = true;  
                 }  
             }  
             a.push("]");  
             return a.join("");  
         } else if ((Object.prototype.toString.call(o) === '[object Date]')) {  
             return '"' + o.getFullYear() + "-" + pad(o.getMonth() +  + "-" + pad(o.getDate()) + "T" + pad(o.getHours()) + ":" + pad(o.getMinutes()) + ":" + pad(o.getSeconds()) + '"';  
         } else if (typeof o == "string") {  
             if (/["\\\x\x]/.test(o)) {  
                 return '"' + o.replace(/([\x\x\\"])/g, function(a, b) {  
                     var c = m[b];  
                     if (c) {  
                         return c;  
                     }  
                     c = b.charCodeAt();  
                     return "\\u + Math.floor(c / .toString( + (c % .toString(;  
                 }) + '"';  
             }  
             return '"' + o + '"';  
         } else if (typeof o == "number") {  
             return isFinite(o) ? String(o) : "null";  
         } else if (typeof o == "boolean") {  
             return String(o);  
         } else {  
             var a = ["{"], b, i, v;  
             for (i in o) {  
                 if (!useHasOwn || o.hasOwnProperty(i)) {  
                     v = o[i];  
                     if (v === null) {  
                         continue;  
                     }  
                     switch (typeof v) {  
                         case "undefined" :  
                         case "function" :  
                         case "unknown" :  
                             break;  
                         default :  
                             if (b) {  
                                 a.push(',');  
                             }  
                             a.push(this.encode(i), ":", this.encode(v));  
                             b = true;  
                     }  
                 }  
             }  
             a.push("}");  
             return a.join("");  
         }  
     });  
 })();  
  
 window.JSON = JSON;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics