87,921
社区成员
发帖
与我相关
我的任务
分享
var str = (this[i] != null) ? this[i].toJSON() : "null";
var str = (this[i] != null) ? this[i].toJSON() : "null";
function JSONstringify(tobj){
var str = typeof tobj.toJSON == "function" ? tobj.toJSON() : OtoJSON(tobj);
return str;
}
function OtoJSON(obj){
var json = [];
for(var i in obj){
if(!obj.hasOwnProperty(i)){
continue;
}
json.push(
JSONstringify(i) + " : " +
((obj[i] != null) ? JSONstringify(obj[i]) : "null")
)
}
return "{" + json.join(",") + "}";
}
Array.prototype.toJSON = function(){
for(var i=0,json=[];i<this.length;i++){
json[i] = (this[i] != null) ? this[i].toJSON() : "null";
}
return "["+json.join(", ")+"]";
};
String.prototype.toJSON = function(){
return '"' +
this.replace(/(\\|\")/g,"\\$1")
.replace(/\n|\r|\t/g,function(){
var a = arguments[0];
return (a == '\n') ? '\\n':
(a == '\r') ? '\\r':
(a == '\t') ? '\\t': ""
}) +
'"';
};
Boolean.prototype.toJSON = function(){return this};
Function.prototype.toJSON = function(){return this};
Number.prototype.toJSON = function(){return this};
RegExp.prototype.toJSON = function(){return this};
var obj = {a:"a",b:123,c:[3,4,5],d:true,e:function(){return false;}};
var strobj = OtoJSON(obj);
alert(strobj);