JS对象转化成JSON字符串
2011-01-31 14:28
477 查看
在工作中需要将JavaScript中的对象转化为字符串,于是使用dojo widget的方式写了一套方法,从而可以实现JS对象转化为字符串,具体实现如下:
调用如上写的dojo widget实现JS对象转化为字符串,如下:
注:其中文件json.js是上边dojo widget实现存放的位置。
dojo.provide("JSONObject"); dojo.require("dijit._Widget"); dojo.declare("JSONObject", [dijit._Widget], { iContext: null, toJSON: function() { var json = []; for (var i in this.iContext) { if (!this.iContext.hasOwnProperty(i)) continue; if (this.iContext[i] == null) json.push( new JSONString({"iContext": i}).toJSON() + " : " + "null" ); else if (typeof this.iContext[i] == "object") json.push( new JSONString({"iContext": i}).toJSON() + " : " + new JSONObject({"iContext": this.iContext[i]}).toJSON() ); else if (typeof this.iContext[i] == "array") json.push( new JSONString({"iContext": i}).toJSON() + " : " + new JSONArray({"iContext": this.iContext[i]}).toJSON() ); else if (typeof this.iContext[i] == "string") json.push( new JSONString({"iContext": i}).toJSON() + " : " + new JSONString({"iContext": this.iContext[i]}).toJSON() ); else if (typeof this.iContext[i] == "boolean" || typeof this.iContext[i] == "function" || typeof this.iContext[i] == "number" || typeof this.iContext[i] == "regexp") json.push( new JSONString({"iContext": i}).toJSON() + " : " + this.iContext[i] ); } return "{/n " + json.join(",/n ") + "/n}"; } }); dojo.declare("JSONArray", [dijit._Widget], { iContext: null, toJSON: function() { for(var i=0,json=[];i<this.iContext.length;i++) if (this.iContext[i] == null) json[i] = "null"; else if (typeof this.iContext[i] == "object") json[i] = new JSONObject({"iContext": this.iContext[i]}).toJSON(); else if (typeof this.iContext[i] == "array") json[i] = new JSONArray({"iContext": this.iContext[i]}).toJSON(); else if (typeof this.iContext[i] == "string") json[i] = new JSONString({"iContext": this.iContext[i]}).toJSON(); else if (typeof this.iContext[i] == "boolean" || typeof this.iContext[i] == "function" || typeof this.iContext[i] == "number" || typeof this.iContext[i] == "regexp") json[i] = this.iContext[i]; return "[" + json.join(", ") + "]"; } }); dojo.declare("JSONString", [dijit._Widget], { iContext: null, toJSON: function() { return '"' + this.iContext.replace(/(//|/")/g,"//$1") .replace(//n|/r|/t/g,function(){ var a = arguments[0]; return (a == '/n') ? '//n': (a == '/r') ? '//r': (a == '/t') ? '//t': ""; }) + '"'; } });
调用如上写的dojo widget实现JS对象转化为字符串,如下:
<html lang="en"> <head> <mce:script djconfig="parseOnLoad: true, isDebug: false" src="dojotoolkit/dojo/dojo.js" mce_src="dojotoolkit/dojo/dojo.js" type="text/javascript"></mce:script> <mce:script type="text/javascript" src="json.js" mce_src="json.js"></mce:script> <mce:script type="text/javascript"><!-- dojo.require("JSONObject"); // --></mce:script> <mce:script type="text/javascript"><!-- function b(){ this.B = "bbbb"; this.B1 = 1111; } function a(){ this.A = "aaaa"; this.AO = new b(); } var strValue = new JSONObject({iContext: new a()}).toJSON(); // --></mce:script> </head> <body> </body> </html>
注:其中文件json.js是上边dojo widget实现存放的位置。
相关文章推荐
- js中的json对象和字符串之间的转化
- 前台的js对象数组传到后台处理。在前台把js对象数组转化为json字符串,在后台把json字符串解析为List<>
- js爬虫拿到的字符串数据转化成数组再转化成json对象
- js中的json对象和字符串之间的转化
- js操作Json数据,JSON对象与字符串转化
- js中的json对象和字符串之间的转化
- JS中JSON对象和JSON字符串的相互转化
- 备用js中将字符串转化为json对象
- js中的json对象和字符串之间的转化
- js转化json数据格式字符串为json对象
- 后台将对象转换为json格式,前台js解析json数据,将字符串转化为数组
- JS对象和JSON字符串相互转化总结
- JS中把字符串转化为JSON对象
- js中json对象和字符串的理解及相互转化操作实现方法
- cocos2d JS 在 JavaScript 中,怎样把一个对象转化成 JSON 字符串?
- js操作Json数据,JSON对象与字符串转化
- js 中json字符串转化json对象
- JS中怎么将json对象转化成字符串
- js中:json对象与字符串转化
- JS中json对象与json字符串互相转化