当前位置: 代码迷 >> JavaScript >> JS中的JSON对象怎么转换为JSON字符串
  详细解决方案

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

热度:217   发布时间:2013-03-01 18:33:02.0
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;
}

  相关解决方案