Converting circular structure to JSON — Any way to

2019-02-03 00:03发布

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.

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?

2条回答
乱世女痞
2楼-- · 2019-02-03 00:43

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

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:

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:

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

In firebug console:

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

In Chrome console:

SCRIPT = document.createElement('script');
SCRIPT.src = '//yandex.st/dojo/1.9.1/dojo/dojo.js';
document.body.appendChild(SCRIPT);
查看更多
淡お忘
3楼-- · 2019-02-03 00:49

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()

insert this line before the JSON.stringify call

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.

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
查看更多
登录 后发表回答