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

JS实现的将对象转为JSON格式代码
这是一个从网上找到的类,我只会把它当成方法使用,如果用类不知怎么用,先记下来

var JS = function(){
    //花未眠
    //bravfing@126.com
    //2011.8.19
    
    var _toStr = function(obj){
        var type = Object.prototype.toString.call(obj).slice(8,-1) , rs;
        
        //如果是html节点(不完全判断,可伪造)
        if(obj.nodeType!=null){
            return "HTMLNODE"
        }
        
        switch(type){
            case "Undefined" : 
            case "Null" : 
            case "Number" :  
            case "Boolean" : 
            case "Date" : 
            case "Function" :
            case "Error" :
            case "RegExp" :  rs = obj ; break;
            
            case "String" : rs = '"' + obj + '"'; break;
            case "Array" :
                rs = "";
                for(var i=0,len=obj.length;i<len;i++){
                    rs+=_toStr(obj[i])+",";
                }
                rs = "[" + rs.slice(0,-1) + "]";
                break;
            
            case "Object" :
                rs = [];    
                for(var k in obj){
                    rs.push('"' + k.toString() + '":' + _toStr(obj[k]));
                }
                rs = "{" + rs.join(",") + "}";
                break;
        }
        return rs;
    };
    
    return {"toStr":_toStr};
}();