JS對象轉化成JSON字串

來源:互聯網
上載者:User

      在工作中需要將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實現存放的位置。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.