且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将JSON转换为字符串时,如何解决此Javascript错误?

更新时间:2022-12-06 20:13:07

一个对象在某处引用了自己.因此,消息为圆形结构".我怀疑它可能在HTMLDivElement中.您是否仅将其用于调试目的,或者您实际上想对该JSON做有意义的事情?如果仅将其用于调试,大多数现代的JavaScript调试器将使您仅将对象记录到控制台.如果您实际上是在尝试对数据进行处理,则应仅从该对象中提取所需的内容,然后将它们放入一个新的经过精简的对象中,然后可以传递给JSON.stringify.该对象看起来像来自Google API,并且其中包含许多额外的数据.

An object is referencing itself somewhere; hence the message "circular structure." I suspect it might be in the HTMLDivElement. Are you using this only for debugging purposes or do you actually want to do something meaningful with this JSON? If you're just using it for debugging, most modern JavaScript debuggers will let you just log an object to the console. If you're actually trying to do something with the data, you should pull out only the things you need from this object and put them into a new trimmed down object that you can pass to JSON.stringify. This object looks like it came from a Google API and has lots of extra data in it.

如果您不介意破坏性地修改对象,请尝试有选择地使可疑字段无效,并查看JSON.stringify是否将接受该对象.至少以这种方式,您会知道是什么原因造成的.请注意,如果这样做,最终可能会损坏该对象,以备将来使用.

If you don't mind destructively modifying the object, try selectively nulling out suspicious fields and see if JSON.stringify will accept the object. At least that way you'll know what's causing it. Note that if you do this you may end up breaking the object for any future uses.