1.Array转换成Json String
function jsonToString(arr) {
var s = "";
if (arr instanceof Array || arr instanceof Object) {
var isObj = 0;
//check value type
for (key in arr) {
if (isNaN(parseInt(key))) { //key is string
isObj = 1;
}
else {
//key is index , check sort
var na = arr.length;
var tmp = arr;
//hack for ie
arr = Array();
for (var j = 0; j < na; j++) {
if (typeof (tmp[j]) == "undefined") {
arr[j] = "";
}
else {
arr[j] = tmp[j];
}
}
}
break;
}
for (key in arr) {
var value = arr[key];
if (isObj) {
if (s) { s += ','; }
s += '"' + key + '":' + jsonToString(value);
}
else {
if (s) { s += ','; }
s += jsonToString(value);
}
}
if (isObj)
s = '{' + s + '}';
else
s = '[' + s + ']'
}
else {
if (!isNaN(parseInt(arr))) {
s += arr;
}
else {
s = '"' + arr + '"';
}
}
return s;
}
2.Json String 转换成 Array:
function stringToJson(json) {
eval_r('var ss=' + json + ';'); return ss;
}