JavaScript 异常:未捕获的类型错误:将循环结构转换为 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10504945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:37:07  来源:igfitidea点击:

JavaScript exception :Uncaught TypeError: Converting circular structure to JSON

javascriptjson

提问by Echo

I hava JSON object :

我有 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:[]}]

I try to stringify it :

我尝试将其字符串化:

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);
}

I use censor as mentioned here :Chrome sendrequest error: TypeError: Converting circular structure to JSONn

我使用这里提到的员:Chrome sendrequest error: TypeError: Converting circle structure to JSONn

However I get the following exception over the browser:

但是,我通过浏览器收到以下异常:

Uncaught TypeError: Converting circular structure to JSON

未捕获的类型错误:将圆形结构转换为 JSON

Here is the Java Script object: enter image description here

这是 Java Script 对象: 在此处输入图片说明

I got the previous JSON object using toSource() at Mozilla browser. Any idea how to fix it !!

我在 Mozilla 浏览器上使用 toSource() 获得了以前的 JSON 对象。任何想法如何解决它!

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

============================更新==================== ===

Actually I need to share with you the scnerio from the beginning: 1 -Initially: I have a form and at the end I build java script object which is :

实际上,我需要从一开始就与您分享 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:[]}]

This object is stringified normally ... NOTE THAT IT"S typical to the one that would fire exception later.

这个对象通常是字符串化的......注意它是典型的稍后会触发异常的对象

2- Then later I delete objects from this array using :

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

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

            resourceVacation.splice(index,1);
    }

3-When I try to stringify that array after I deleted a single object , I get the mentioned exception. So .. anu ideas why does it passed the first time and failed 2nd time !!

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

回答by dontGoPlastic

You can't JSON encode date objects.

您不能对日期对象进行 JSON 编码。

From json.org: "A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested."

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

回答by user2102131

The problem is the source - object which is a circular reference.

问题是源对象是一个循环引用

You should create a copy of the object without the source object.

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

That's how I solved the issue in FullCalendar.

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