Object.prototype.deep_clone = function(){
eval("var tmp = " + this.toJSON());
return tmp;
}
Object.prototype.toJSON = function(){
var json = [];
for(var i in this){
if(!this.hasOwnProperty(i)){
continue;
}
//if(typeof this[i] == "function") continue;
else{
var str = (this[i] != null) ? this[i].toJSON() : "null";
json.push(i.toJSON() + " : " + str);
}
}
return "{\n " + json.join(",\n ") + "\n}";
}
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}// strict but slow
String.prototype.toJSON = function(){
var tmp = this.split("");
for(var i=0;i<tmp.length;i++){
var c = tmp[i];
(c >= ' ') ?
(c == '\\') ? (tmp[i] = '\\\\'):
(c == '"') ? (tmp[i] = '\\"' ): 0 :
(tmp[i] =
(c == '\n') ? '\\n' :
(c == '\r') ? '\\r' :
(c == '\t') ? '\\t' :
(c == '\b') ? '\\b' :
(c == '\f') ? '\\f' :
(c = c.charCodeAt(),('\\u00' + ((c>15)?1:0)+(c%16)))
)
}
return '"' + tmp.join("") + '"';
}
=========运行下面的程序,第一次没有错误,在刷新就报红色字体的错误,在ie7/ff3下.var json = {
str : "abcde",
num : 6,
reg : /foobar/i,
array : [1,2,3,4,5],
func : function(x,y){return x+y},
obj : {a : "b"}
}.toJSON();alert(json);
// result
{
"str" : "abcde",
"num" : 6,
"reg" : /foobar/i,
"array" : [1, 2, 3, 4, 5],
"func" : function(x,y){return x+y},
"obj" : {
"a" : "b"
}
}
eval("var tmp = " + this.toJSON());
return tmp;
}
Object.prototype.toJSON = function(){
var json = [];
for(var i in this){
if(!this.hasOwnProperty(i)){
continue;
}
//if(typeof this[i] == "function") continue;
else{
var str = (this[i] != null) ? this[i].toJSON() : "null";
json.push(i.toJSON() + " : " + str);
}
}
return "{\n " + json.join(",\n ") + "\n}";
}
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}// strict but slow
String.prototype.toJSON = function(){
var tmp = this.split("");
for(var i=0;i<tmp.length;i++){
var c = tmp[i];
(c >= ' ') ?
(c == '\\') ? (tmp[i] = '\\\\'):
(c == '"') ? (tmp[i] = '\\"' ): 0 :
(tmp[i] =
(c == '\n') ? '\\n' :
(c == '\r') ? '\\r' :
(c == '\t') ? '\\t' :
(c == '\b') ? '\\b' :
(c == '\f') ? '\\f' :
(c = c.charCodeAt(),('\\u00' + ((c>15)?1:0)+(c%16)))
)
}
return '"' + tmp.join("") + '"';
}
=========运行下面的程序,第一次没有错误,在刷新就报红色字体的错误,在ie7/ff3下.var json = {
str : "abcde",
num : 6,
reg : /foobar/i,
array : [1,2,3,4,5],
func : function(x,y){return x+y},
obj : {a : "b"}
}.toJSON();alert(json);
// result
{
"str" : "abcde",
"num" : 6,
"reg" : /foobar/i,
"array" : [1, 2, 3, 4, 5],
"func" : function(x,y){return x+y},
"obj" : {
"a" : "b"
}
}
解决方案 »
- 独立文件中的javascript为什么不能正常工作?
- JS实现菜单TOP值的随机显示,高手请进场指导!!!!!
- javascript动态生成按钮后,如何绑定onclick事件
- 浏览器为什么会请求favicon.ico文件
- 一个关于下拉框和列表框绑定的简单问题
- 如何用javascript设置body里的leftmargin属性?
- 点一下按钮,自动增加一行单格。脚本如何实现。谢谢
- <A href="" onclick="window.back();">back</A>错误
- 讨论!!怎样在网页中实现录音的功能,并能让用户听到自己的声音
- 这段js是用什么方式加密的?试了好几种方法都不能解开
- 如何使SELECT 下边框下的空白部分不闪动?
- iframe 中的链接打开新窗口
var str = (this[i] != null) ? this[i].toJSON() : "null";
应该是三元表达式中this[i].toJSON()这里出错了
这一句三元表达式在JS里并不能避免执行this[i].toJSON()这一句,不管条件如何这一句总是要执行的,而当你的this[i]为null时就出错了
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);