js JsonToString

时间:2024-03-01 09:28:43
function JsonToString(o) { 
    var arr = []; 
    var fmt = function(s) { 
        if (typeof s == \'object\' && s != null) return JsonToString(s); 
        return /^(string)$/.test(typeof s) ? \'"\' + s + \'"\' : s;
    } 
    if(o.length>0){
        for(var i=0;i<o.length;i++){
            arr.push(fmt(o[i])); 
        }
        return \'[\' + arr.join(\',\') + \']\'; 
    }else{
        for (var i in o) 
            arr.push(\'"\' + i + \'":\' + fmt(o[i])); 
        return \'{\' + arr.join(\',\') + \'}\'; 
    }

}