在工作中需要將JavaScript中的對象轉化為字串,於是使用dojo widget的方式寫了一套方法,從而可以實現JS對象轉化為字串,具體實現如下:
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 />});
調用如上寫的dojo widget實現JS對象轉化為字串,如下:
<html lang="en"><br /><head><br /><mce:script djconfig="parseOnLoad: true, isDebug: false" src="dojotoolkit/dojo/dojo.js" mce_src="dojotoolkit/dojo/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 a()}).toJSON();</p><p>// --></mce:script><br /></head><br /><body><br /></body><br /></html>
註:其中檔案json.js是上邊dojo widget實現存放的位置。