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"
}
}

解决方案 »

  1.   

    见鬼了,我在家里调式也没有问题,但是在公司用vs2008调式,和用ff3调试都报错...
      

  2.   

    谢谢大家的支持,是jquery搞的鬼,只要引用了jquery就报错....谁能帮忙解决下.
      

  3.   

    把下面这一句三元表达式改成用if语句实现
    var str = (this[i] != null) ? this[i].toJSON() : "null";
    应该是三元表达式中this[i].toJSON()这里出错了
      

  4.   

    var str = (this[i] != null) ? this[i].toJSON() : "null";
    这一句三元表达式在JS里并不能避免执行this[i].toJSON()这一句,不管条件如何这一句总是要执行的,而当你的this[i]为null时就出错了
      

  5.   

    这样改对象......参见这里的JSON转换:http://www.easyui.org.cn/#toJSON
      

  6.   

    那个toJSON函数有隐患的,我修正了一下,这样用就没问题了: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);