JavaScript异常:未捕获TypeError:将循环结构转换为JSON

时间:2012-05-08 19:12:17

标签: javascript json

我hava JSON对象:

[#1={id:"2012-05-04", title:"Scheduled", start:(new Date(1336096800000)), source:{events:[#1#], className:[]}, _id:"2012-05-04", _start:(new Date(1336089600000)), end:null, _end:null, allDay:true, className:[]}]

我尝试将其字符串化:

var test = JSON.stringify(resourceVacation, censor(resourceVacation));

function censor(censor) {
    return (function() {
        var i = 0;
        return function(key, value) {
            if (i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value)
                return '[Circular]';

            ++i; // so we know we aren't using the original object anymore

            return value;
        }
    })(censor);
}

我使用此处提到的审查员:Chrome sendrequest error: TypeError: Converting circular structure to JSON n

但是我通过浏览器获得以下异常:

  

未捕获的TypeError:将循环结构转换为JSON

这是Java Script对象: enter image description here

我在Mozilla浏览器中使用toSource()获得了以前的JSON对象。 知道怎么解决它!!

============================ UPDATE ================== ==

其实我需要从头开始与你分享scnerio:   1 - 最初:我有一个表单,最后我构建了java脚本对象,它是:

#1=[{id:"2012-05-03", title:"Scheduled", start:(new Date(1336010400000)), source:{events:#1#, className:[]}, _id:"2012-05-03", _start:(new Date(1336003200000)), end:null, _end:null, allDay:true, className:[]}]

此对象通常是字符串化的...请注意,“对于稍后会触发异常的来说,这是典型的。

2-然后我使用以下方法从该数组中删除对象:

function deleteVacation(day) {
    for (var index = 0; index < resourceVacation.length; index++) {
        if (resourceVacation[index].id == day)

            resourceVacation.splice(index,1);
    }

3 - 当我在删除单个对象后尝试对该数组进行字符串化时,我得到了提到的异常。 所以... anu想法为什么它第一次通过而第二次失败!!

2 个答案:

答案 0 :(得分:8)

您无法对日期对象进行JSON编码。

来自json.org:“值可以是双引号中的字符串,或数字,或true或false或null,或对象或数组。这些结构可以嵌套。”

答案 1 :(得分:8)

问题是来源 - object which is a circular reference

您应该创建没有源对象的对象副本。

这就是我在FullCalendar中解决问题的方法。