日期:2014-05-16 浏览次数:20409 次
var arr = new Array();
arr.type = "common";
arr.address = "china";
var arr1 = new Array();
arr1.type = "common";
arr1.content = "hello,我来自金星";
arr.content = arr1;
var json = [
{a:[{a:1,b:2},
{c:3,d:4}]},
{b:[{a:1,b:2},
{c:3,d:4}]},
{c:[{a:1,b:2},
{c:3,d:4}]},
]
function serialize(o)
{
var result = "";
var tempResult = [];
if(o instanceof Array){
for(var i = 0 ; i < o.length ; i ++)
{
tempResult.push(serialize(o[i]));
}
result = '['+tempResult.join(',')+']';
}
else
{
for(var key in o)
{
if(o[key] instanceof Array) tempResult.push(key+":"+serialize(o[key]));
else tempResult.push(key+":"+o[key]);
}
result = '{'+tempResult.join(',')+'}'
}
return result;
}
alert(serialize(json))
------解决方案--------------------
json2.js
JSON.stringify && JSON.parse
------解决方案--------------------
最好的办法是加入文件json2,直接有一个内置的方法。
JSON.parse()
------解决方案--------------------
为什么不用store和model来存储信息呢?
这样转json也容易。