How to convert JavaScript object to string

 

function objectToString(o){    
    var parse = function(_o){    
        var a = [], t;  
        if(Array.isArray(_o)){
        	for(var i = 0, t; t = _o[i]; i ++){
        		a[a.length] = "{ " + arguments.callee(t).join(", ") + "}";
        	}
        } else {
	        for(var p in _o){        
	            if(_o.hasOwnProperty(p)){            
	                t = _o[p];
	                if(!t){
	                	t = '';
	                }
	                switch(typeof t){
	                	case "object":
	                		if(Array.isArray(t)){
	                			a[a.length] = "\"" + p + "\": [ " + arguments.callee(t).join(", ") + "]";
	                		} else {
	                			a[a.length] = "\"" + p + "\":{ " + arguments.callee(t).join(", ") + "}";
	                		}                		
	                		break;
	                	case "number":
	                		a[a.length] = [ "\"" + p+ "\": " + t.toString() + "" ];
	                		break;
	                	case "string":
	                		a[a.length] = [ "\"" + p+ "\": \"" + t.toString() + "\"" ];
	                		break;
	                }
	            }
	        }        
        }
        return a;        
    }
    
    return "{" + parse(o).join(", ") + "}";    
}

 

from 

http://www.davidpirek.com/blog/object-to-string-how-to-deserialize-json

你可能感兴趣的:(JavaScript)