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;
}
{
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;
}
本文介绍了一种使用JavaScript实现的对象序列化方法,该方法能够处理包括数组和对象在内的各种数据结构,并通过递归调用确保复杂对象也能被正确转换为字符串形式。

被折叠的 条评论
为什么被折叠?



