多維數組轉json

Object.extendJson = function(A, C) {
for ( var B in C) {
A[B] = C[B];
}
return A;
};
Object.keys = function(C) {
var A = [];
for ( var B in C) {
A.push(B);
}
return A;
};
Object.values = function(C) {
var A = [];
for ( var B in C) {
A.push(C[B]);
}
return A;
};
Object.serialize2Str = function(D) {
if (D == null) {
return null;
}
if (D.serialize2Str) {
return D.serialize2Str();
}
var B = D.constructor;
switch (B) {
case String:
return '"' + D.encode4Js() + '"';
case Number:
return D + "";
case Boolean:
return D + "";
case Date:
return "new Date(" + D.getTime() + ")";
case Array:
var A = [];
for ( var C = 0; C < D.length; C++) {
A[C] = Object.serialize2Str(D[C]);
}
return "[" + A.join(",") + "]";
case Object:
var A = [];
for ( var C in D) {
A.push('"' + (C + "").encode4Js() + '":'
+ Object.serialize2Str(D[C]));
}
return "{" + A.join(",") + "}";
}
return null;
};
   
Date.prototype.format = function(B) {
B = B || "yyyy-MM-dd";
var C = {
"M+" : this.getMonth() + 1,
"d+" : this.getDate(),
"h+" : this.getHours(),
"m+" : this.getMinutes(),
"s+" : this.getSeconds(),
"q+" : Math.floor((this.getMonth() + 3) / 3),
"S" : this.getMilliseconds()
};
if (/(y+)/.test(B)) {
B = B.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for ( var A in C) {
if (new RegExp("(" + A + ")").test(B)) {
B = B.replace(RegExp.$1, RegExp.$1.length == 1 ? C[A]
: ("00" + C[A]).substr(("" + C[A]).length));
}
}
return B;
};
String.prototype.trim = function() {
return this.replace(/(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+$)/g, "");
};
String.prototype.format = function() {
if (arguments.length == 0) {
return this;
}
for ( var B = this, A = 0; A < arguments.length; A++) {
B = B.replace(new RegExp("\\{" + A + "\\}", "g"), arguments[A]);
}
return B;
};
String.prototype.dbc2sbc = function() {
return this.replace(/[\uff01-\uff5e]/g, function(A) {
return String.fromCharCode(A.charCodeAt(0) - 65248);
}).replace(/\u3000/g, " ");
};
String.prototype.parseQuery = function(A) {
var B = new RegExp("(^|&|\\?)" + A + "=([^&]*)(&|$)", "i"), C;
if (C = this.match(B)) {
return C[2];
}
return null;
};
String.prototype.byteLen = function() {
return this.replace(/[^\x00-\xff]/g, "--").length;
};
String.prototype.camelize = function() {
return this.replace(/\-(\w)/ig, function(B, A) {
return A.toUpperCase();
});
};
String.prototype.capitalize = function() {
return this.substr(0, 1).toUpperCase() + this.substr(1);
};
String.prototype.encode4Js = function() {
var C = [ /\\/g, /"/g, /'/g, /\//g, /\r/g, /\n/g, /\t/g ];
var D = [ "\\u005C", "\\u0022", "\\u0027", "\\u002F", "\\u000A", "\\u000D",
"\\u0009" ];
var B = this;
for ( var A = 0; A < C.length; A++) {
B = B.replace(C[A], D[A]);
}
return B;
};
String.prototype.encode4Html = function() {
var B = document.createElement("div");
var A = document.createTextNode(this);
B.appendChild(A);
return B.innerHTML.replace(/"/g, """).replace(/'/g, "'");
};
String.prototype.decode4Html = function() {
var A = document.createElement("div");
A.innerHTML = this.stripTags() + "<a/>";
return A.childNodes[1] ? A.childNodes[0].nodeValue + "" : "";
};
String.prototype.stripTags = function() {
return this.replace(/<(?:'[^']*'|"[^"]*"|[^"'>])*>/g, "");
};
String.prototype.subByte = function(A, B) {
var C = this;
if (C.byteLen() <= A) {
return C;
}
B = B || "";
A -= B.byteLen();
return C = C.substr(0, A).replace(/([^\x00-\xff])/g, "$1 ").substr(0, A)
.replace(/[^\x00-\xff]$/, "").replace(/([^\x00-\xff]) /g, "$1")
+ B;
};
RegExp.escape = function(A) {
return String(A).replace(/([.*+?^=!:${}()|[\]\/\\])/g, "\\$1");
};
Object.asPrototype = function(B) {
var A = function() {
};
A.prototype = B;
return A;
};
Object.definedInCore = function(A) {
while (A.$superClass) {
if (A == A.$superClass) {
break;
}
A = A.$superClass;
}
return A === Array || A === Boolean || A === Date || A === Error
|| A === EvalError || A === Function || A === Math || A === Number
|| A === Object || A === RangeError || A === RegExp || A === String
|| A === SyntaxError || A === TypeError || A === URIError;
};
Object.implementationOf = function(E, B) {
var D = E && Object.getClass(E).__interfaces__;
if (D) {
for ( var C = 0, A = D.length; C < A; C++) {
if (D[C] == B) {
return true;
}
}
}
return false;
};
Object.instanceOf = function(A, B) {
if (B == null) {
return false;
}
if (typeof (B) == "string") {
return typeof (A) == B;
}
if (B && B instanceof Function && A instanceof B) {
return true;
}
if (B && A && A.constructor == B) {
return true;
}
if (B
&& A
&& B.constructor
&& A.constructor
&& Object.prototypeOf(B.prototype,
A.constructor.prototype.constructor)) {
return true;
}
return Object.implementationOf(A, B);
};
Object.prototypeOf = function(B, C) {
if (!B || !C) {
return false;
}
var A = C;
while (true) {
if (!A.prototype) {
return false;
}
if (B == A.prototype) {
return true;
}
if (!A.prototype.constructor || A == A.prototype.constructor) {
return false;
}
A = A.prototype.constructor;
}
};
Object.getClass = function(A) {
if (A.$class instanceof Function) {
return A.$class;
}
return A.constructor;
};
使用方法:使用方法: data =  Object.serialize2Str(data);
第二種var jsontext=JSON.stringify(all_class); //json格式化
all_class=JSON.parse(jsontext);  //轉回二維數組,PS:json開始與結尾沒有雙引號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章