日期:2014-05-16  浏览次数:20463 次

JS中的JSON对象如何转换为JSON字符串
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;
}