Convert a JS object to a JSON string

Source: Internet
Author: User
Tags tojson

In work, JavaScript objects need to be converted into strings. Therefore, a method is written using the dojo widget to convert JavaScript objects into strings. The specific implementation is as follows:

Dojo. provide ("jsonobject"); </P> <p> dojo. require ("dijit. _ widget "); </P> <p> dojo. declare ("jsonobject", [dijit. _ widget], {<br/> icontext: NULL, <br/> tojson: function () {<br/> var JSON = []; <br/> for (var I in this. icontext) {<br/> If (! This. icontext. hasownproperty (I) continue; <br/> If (this. icontext [I] = NULL) <br/> JSON. push (<br/> New jsonstring ({"icontext": I }). tojson () + ":" + <br/> "null" <br/>); <br/> else if (typeof this. icontext [I] = "object") <br/> JSON. push (<br/> New jsonstring ({"icontext": I }). tojson () + ":" + <br/> New jsonobject ({"icontext": This. icontext [I]}). tojson () <br/>); <br/> else if (typeof this. icontext [I] = = "Array") <br/> JSON. push (<br/> New jsonstring ({"icontext": I }). tojson () + ":" + <br/> New jsonarray ({"icontext": This. icontext [I]}). tojson () <br/>); <br/> else if (typeof this. icontext [I] = "string") <br/> JSON. push (<br/> New jsonstring ({"icontext": I }). tojson () + ":" + <br/> New jsonstring ({"icontext": This. icontext [I]}). tojson () <br/>); <br/> else if (typeof this. icontext [I] = "Boolean" | <br /> Typeof this. icontext [I] = "function" | <br/> typeof this. icontext [I] = "Number" | <br/> typeof this. icontext [I] = "Regexp") <br/> JSON. push (<br/> New jsonstring ({"icontext": I }). tojson () + ":" + <br/> This. icontext [I] <br/>); <br/>}< br/> return "{/N" + JSON. join (",/N") + "/n}"; <br/>}< br/>}); </P> <p> dojo. declare ("jsonarray", [dijit. _ widget], {<br/> icontext: NULL, <br/> tojson: function () {<Br/> for (VAR I = 0, JSON = []; I <this. icontext. length; I ++) <br/> If (this. icontext [I] = NULL) <br/> JSON [I] = "null"; <br/> else if (typeof this. icontext [I] = "object") <br/> JSON [I] = new jsonobject ({"icontext": This. icontext [I]}). tojson (); <br/> else if (typeof this. icontext [I] = "array") <br/> JSON [I] = new jsonarray ({"icontext": This. icontext [I]}). tojson (); <br/> else if (typeof this. icontext [I] =" String ") <br/> JSON [I] = new jsonstring ({" icontext ": This. icontext [I]}). tojson (); <br/> else if (typeof this. icontext [I] = "Boolean" | <br/> typeof this. icontext [I] = "function" | <br/> typeof this. icontext [I] = "Number" | <br/> typeof this. icontext [I] = "Regexp") <br/> JSON [I] = This. icontext [I]; <br/> return "[" + JSON. join (",") + "]"; <br/>}< br/>}); </P> <p> dojo. declare ("jsonstring", [dijit. _ Widget], {<br/> icontext: NULL, <br/> tojson: function () {<br/> return '"' + this. icontext. replace (/(// |/")/g," // $1 ") <br/>. replace (// n |/R |/T/g, function () {<br/> var A = arguments [0]; <br/> return (A = '/N ')? '// N': <br/> (A ='/R ')? '// R': <br/> (A ='/t ')? '// T': ""; <br/>}) +' "'; <br/>}< br/> });

Call the dojo widget written above to convert JS objects to strings, as shown below:

<HTML lang = "en"> <br/> <pead> <br/> <MCE: script djconfig = "parseonload: True, isdebug: false "src =" dojotoolkit/dojo. JS "mce_src =" dojotoolkit/dojo. JS "type =" text/JavaScript "> </MCE: SCRIPT> <br/> <MCE: Script Type =" text/JavaScript "src =" JSON. JS "mce_src =" JSON. JS "> </MCE: SCRIPT> <br/> <MCE: Script Type =" text/JavaScript "> <! -- </P> <p> dojo. require ("jsonobject"); </P> <p> // --> </MCE: SCRIPT> <br/> <MCE: script Type = "text/JavaScript"> <! -- </P> <p> function B () {<br/> This. B = "BBBB"; <br/> This. b1 = 1111; <br/>}</P> <p> function a () {<br/> This. A = "aaaa"; <br/> This. ao = new B (); <br/>}</P> <p> var strvalue = new jsonobject ({icontext: New ()}). tojson (); </P> <p> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> </ptml>

Note: The JSON. js file is the storage location of the above dojo widget.

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.