功能如题,代码如下
var json2str = function(json){ var type = typeof json; if(type === 'number' || type === 'boolean'){ return json; }else if(json === null){ return 'null'; }else if(type === 'string'){ return '"' + json.toString() + '"'; }else if(type === 'function'){ return 'null'; }else if(type === 'object'){ if(json instanceof Date){ return '"' + json.toString() + '"'; }else if(json instanceof Array){ var arr = []; var jlen = json.length; for(var i = 0; i < jlen; i++){ arr.push(arguments.callee(json[i])); } console.log(arr); return '[' + arr.join(',') + ']'; }else{ var obj = []; for(var key in json){ obj.push('"' + key + '"' + ':' + arguments.callee(json[key])); } return '{' + obj.join(',') + '}'; } } }