js将对象转成json格式

/**
 * 序列化成json字符串
 * @param obj
 * @return
 */
function Serialize(obj){     
    switch(obj.constructor){     
        case Object:     
            var str = "{";     
            for(var o in obj){     
                str += o + ":" + Serialize(obj[o]) +",";     
            }     
            if(str.substr(str.length-1) == ",")     
                str = str.substr(0,str.length -1);     
             return str + "}";     
             break;     
         case Array:                 
             var str = "[";     
             for(var o in obj){     
                 str += Serialize(obj[o]) +",";     
             }     
             if(str.substr(str.length-1) == ",")     
                 str = str.substr(0,str.length -1);     
             return str + "]";     
             break;     
         case Boolean:     
             return "\"" + obj.toString() + "\"";     
             break;     
         case Date:     
             return "\"" + obj.toString() + "\"";     
             break;     
         case Function:     
             break;     
         case Number:     
             return "\"" + obj.toString() + "\"";     
             break;      
         case String:     
             return "\"" + obj.toString() + "\"";     
             break;         
     }     
 }  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。