Simple implementation case of JS object to JSON array

Source: Internet
Author: User
Tags join json

This article is mainly to the JS object to the JSON array of simple implementation of the case is introduced, the need for friends can come to the reference, I hope to help you

The code is as follows: 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, "$". 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 (); }  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.