Posted in Javascript onFebruary 28, 2014
function obj2Str(obj){ switch(typeof(obj)){ case 'object': var ret = []; if (obj instanceof Array){ for (var i = 0, len = obj.length; i < len; i++){ ret.push(obj2Str(obj[i])); } return '[' + ret.join(',') + ']'; } else if (obj instanceof RegExp){ return obj.toString(); } else{ for (var a in obj){ ret.push(a + ':' + obj2Str(obj[a])); } return '{' + ret.join(',') + '}'; } case 'function': return 'function() {}'; case 'number': return obj.toString(); case 'string': return "\"" + obj.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function(a) {return ("\n"==a)?"\\n":("\r"==a)?"\\r":("\t"==a)?"\\t":"";}) + "\""; case 'boolean': return obj.toString(); default: return obj.toString(); } }
js对象转json数组的简单实现案例
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@