您的位置:首页 > Web前端 > JavaScript

JS对象转化成JSON字符串

2011-01-31 14:28 477 查看
在工作中需要将JavaScript中的对象转化为字符串,于是使用dojo widget的方式写了一套方法,从而可以实现JS对象转化为字符串,具体实现如下:

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实现存放的位置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: