ArrayToJson converts the array to json format js Code. For more information, see. Function Code:
The Code is as follows:
Function arrayToJson (o ){
Var r = [];
If (typeof o = "string") return "\" "+ o. replace (/([\ '\ "\])/g," \ $1 "). replace (/(\ n)/g, "\ n "). replace (/(\ r)/g, "\ r "). replace (/(\ t)/g, "\ t") + "\"";
If (typeof o = "object "){
If (! O. sort ){
For (var I in o)
R. push (I + ":" + arrayToJson (o [I]);
If (!! Document. all &&! /^ \ N? Function \ s * toString \ (\) \ s * \ {\ n? \ S * \ [native code \] \ n? \ S * \} \ n? \ S * $/. test (o. toString )){
R. push ("toString:" + o. toString. toString ());
}
R = "{" + r. join () + "}";
} Else {
For (var I = 0; I <o. length; I ++ ){
R. push (arrayToJson (o [I]);
}
R = "[" + r. join () + "]";
}
Return r;
}
Return o. toString ();
}
DEMO code:
Script function arrayToJson (o) {var r = []; if (typeof o = "string") return "\" "+ o. replace (/([\ '\ "\])/g," \ $1 "). replace (/(\ n)/g, "\ n "). replace (/(\ r)/g, "\ r "). replace (/(\ t)/g, "\ t") + "\" "; if (typeof o =" object ") {if (! O. sort) {for (var I in o) r. push (I + ":" + arrayToJson (o [I]); if (!! Document. all &&! /^ \ N? Function \ s * toString \ (\) \ s * \ {\ n? \ S * \ [native code \] \ n? \ S * \} \ n? \ S * $ /. test (o. toString) {r. push ("toString:" + o. toString. toString ();} r = "{" + r. join () + "}" ;}else {for (var I = 0; I <o. length; I ++) {r. push (arrayToJson (o [I]);} r = "[" + r. join () + "]";} return r;} return o. toString ();} var str2 = ", SC .jb51.net, clip download, Baidu, 456,456,456"; var arr1 = str2.split (","); alert (arrayToJson (arr1 )); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]