Home > Web Front-end > JS Tutorial > Simple implementation case of converting js object to json array_javascript skills

Simple implementation case of converting js object to json array_javascript skills

WBOY
Release: 2016-05-16 16:58:01
Original
1308 people have browsed it
复制代码 代码如下:

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();
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template