首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JS中的JSON对象怎么转换为JSON字符串

2013-03-01 
JS中的JSON对象如何转换为JSON字符串function serialize(o){var result var tempResult []if(o in

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;
}

热点排行