将数组转换成json格式(超牛)

function Item(){
this.programFolder=new Object();
this.programSite=new Object();
this.materialFolder=new Object();
this.materialSite=new Object();
}
var str = "({test:{programFolder:11, programSite:22, materialFolder:33, materialSite:44}})";
var a={};
a["test"]=new Item();
a.test.programFolder=11;
a.test.programSite=22;
a.test.materialFolder=33;
a.test.materialSite=44;

function obj2str(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+":"+obj2str(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 r.push(obj2str(o[i]));
}
r="["+r.join()+"]";
}
return r;
}
return o.toString();
}
$(document).ready(function(){
$('#test').click(function(){
var b = eval(str);
alert(obj2str(b));
});
})

你可能感兴趣的:(数据格式)