json 对象 转字符串
JSON.Json2Str = JSON.Json2Str || function (obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
if (t == "string") obj = '"' + obj + '"';
return String(obj);
}
else {
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n]; t = typeof (v);
if (t == "string") v = '"' + v + '"';
else if (t == "object" && v !== null) v = JSON.Json2Str(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};
var tmp = { one: 1, two: "2" };
JSON.Json2Str(tmp); // '{"one":1,"two":"2"}'