日期:2014-05-16 浏览次数:20398 次
var arr = [
{id:'1'},
{id:'2'}
];
var json = JSON.stingify(arr);
function toJson(obj){
var toStr = function(v){
if(v==undefined) return v;
switch(v.constructor){
case Number:
case Boolean:
return v;
case String:
return '"' + v.replace(/"/g,'\\"') + '"';
case Array:
var isA=1;
case Object:
var ss=[]
for(var i in v) ss.push('"'+i+'":'+toStr(v[i]));
return (isA?'[':'{') + ss.join(",") + (isA?']':'}');
default:
return undefined;
}
};
return toStr(obj);
}
alert( toJson([1, {a:1,b:2} ]) )