将循环结构转换为JSON——有什么方法可以找到它抱怨的字段吗?

时间:2021-05-30 16:57:10

I'm trying to stringify(...) an object in Chrome, and I keep getting a "Converting circular structure to JSON" message, despite the fact that (as far as I know) no such structure exists.

我正在尝试在Chrome中对一个对象进行stringify(…),我不断地得到一条“将循环结构转换为JSON”的消息,尽管(据我所知)不存在这样的结构。

I've been over the code a dozen times and can't find any circular references whatsoever. Is there any way to get Chrome to tell me what it's bitching about beyond this painfully useless error message?

我已经检查过代码十几次了,但是找不到任何循环引用。有什么办法能让Chrome告诉我,除了这个毫无用处的错误信息,它还在抱怨什么呢?

2 个解决方案

#1


31  

Pardon me if this is too obvious. At the time of writing, I dont know what you have tried.

请原谅我说得太明显了。在写作的时候,我不知道你试过什么。

insert

插入

console.log(the object); 

replacing 'the object' with the object you are passing to JSON.stringify()

将“对象”替换为传递给JSON.stringify()的对象

insert this line before the JSON.stringify call

在JSON之前插入这一行。stringify电话

and look in the console log (shift control J) for the object. In the console log the object will be tagged with a ">" symbol which can be clicked to expand to the fields.

并查看该对象的控制台日志(shift control J)。在控制台日志中,对象将被标记为“>”符号,可以单击该符号将其扩展到字段。

It is complaining about an object that has pointers into itself, like this kind of object:

它在抱怨一个对象,它有指向自身的指针,就像这样的对象:

A = [];
A[0] = A; 
JSON.stringify(A); // circular error

#2


2  

You can use dojox.json.ref to find circular references. This code prints json representation of your objectWithCircularReferences:

您可以使用dojox.json。查找循环引用。此代码打印您的objectWithCircularReferences的json表示形式:

require(["dojox/json/ref"], function(){
    console.log(dojox.json.ref.toJson(objectWithCircularReferences));
});

Any occurence of "$ref" substring in its output to console will help you locate circular references. You can alternatively pipe this json output to global variable ZZZ like this if you wish:

在输出到控制台的任何“$ref”子字符串的出现都将帮助您定位循环引用。如果您希望:

require(["dojox/json/ref"], function(){
    window.ZZZ = dojox.json.ref.toJson(objectWithCircularReferences);
});

And of course you need to include dojo library beforehand. In an html file:

当然,您需要预先包含dojo库。在html文件:

<script src="//yandex.st/dojo/1.9.1/dojo/dojo.js"></script>

In firebug console:

在firebug控制台:

include("//yandex.st/dojo/1.9.1/dojo/dojo.js")

In Chrome console:

在Chrome控制台:

SCRIPT = document.createElement('script');
SCRIPT.src = '//yandex.st/dojo/1.9.1/dojo/dojo.js';
document.body.appendChild(SCRIPT);

#1


31  

Pardon me if this is too obvious. At the time of writing, I dont know what you have tried.

请原谅我说得太明显了。在写作的时候,我不知道你试过什么。

insert

插入

console.log(the object); 

replacing 'the object' with the object you are passing to JSON.stringify()

将“对象”替换为传递给JSON.stringify()的对象

insert this line before the JSON.stringify call

在JSON之前插入这一行。stringify电话

and look in the console log (shift control J) for the object. In the console log the object will be tagged with a ">" symbol which can be clicked to expand to the fields.

并查看该对象的控制台日志(shift control J)。在控制台日志中,对象将被标记为“>”符号,可以单击该符号将其扩展到字段。

It is complaining about an object that has pointers into itself, like this kind of object:

它在抱怨一个对象,它有指向自身的指针,就像这样的对象:

A = [];
A[0] = A; 
JSON.stringify(A); // circular error

#2


2  

You can use dojox.json.ref to find circular references. This code prints json representation of your objectWithCircularReferences:

您可以使用dojox.json。查找循环引用。此代码打印您的objectWithCircularReferences的json表示形式:

require(["dojox/json/ref"], function(){
    console.log(dojox.json.ref.toJson(objectWithCircularReferences));
});

Any occurence of "$ref" substring in its output to console will help you locate circular references. You can alternatively pipe this json output to global variable ZZZ like this if you wish:

在输出到控制台的任何“$ref”子字符串的出现都将帮助您定位循环引用。如果您希望:

require(["dojox/json/ref"], function(){
    window.ZZZ = dojox.json.ref.toJson(objectWithCircularReferences);
});

And of course you need to include dojo library beforehand. In an html file:

当然,您需要预先包含dojo库。在html文件:

<script src="//yandex.st/dojo/1.9.1/dojo/dojo.js"></script>

In firebug console:

在firebug控制台:

include("//yandex.st/dojo/1.9.1/dojo/dojo.js")

In Chrome console:

在Chrome控制台:

SCRIPT = document.createElement('script');
SCRIPT.src = '//yandex.st/dojo/1.9.1/dojo/dojo.js';
document.body.appendChild(SCRIPT);