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.