This article describes how to convert a json object to a string. For more information, see
The Code is as follows:
/*
Converts a json object to a string.
Usage:
Var json = {id = 3, name = "abc "};
Var str = tools. j2s (json );
*/
Tools. j2s = function (O ){
Var S = [];
Var J = "";
If (Object. prototype. toString. apply (O) = '[object Array]') {
For (var I = 0; I <O. length; I ++ ){
S. push (this. j2s (O [I]);
}
J = '[' + S. join (',') + ']';
} Else if (Object. prototype. toString. apply (O) = '[object Date]') {
J = "new Date (" + O. getTime () + ")";
} Else if (Object. prototype. toString. apply (O) = '[object RegExp]' | Object. prototype. toString. apply (O) = '[object Function]') {
J = O. toString ();
} Else if (Object. prototype. toString. apply (O) = '[object Object]') {
For (var I in O ){
Var tempObj = "";
If (typeof (O [I]) = 'string '){
TempObj = '"' + O [I] + '"';
} Else if (typeof (O [I]) = 'object '){
TempObj = this. j2s (O [I]);
} Else {
TempObj = O [I];
}
S. push ('"' + I + '":' + tempObj );
}
J = '{' + S. join (',') + '}';
} Else if (Object. prototype. toString. apply (O) = '[object String]') {
J = '"' + O + '"';
} Else {
J = O;
}
Return J;
};
// Convert a json string to a json object
Tools. s2j = function (jsonString ){
If (jsonString = null | jsonString = ""){
JsonString = "{}";
}
Return eval ('+ jsonString + ')');
};
// JSON string to Object
Tools. json2Obj = function (_ json ){
If (_ json = ''){
// Alert ("function initialization failed! ");
Return "";
}
Return eval ("({root:" + _ json + "})");
};