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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4954242/
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-10-25 15:28:00  来源:igfitidea点击:

How do I solve this Javascript error when converting JSON to string?

javascriptjquerystringjsonparsing

提问by TIMEX

Uncaught TypeError: Converting circular structure to JSON

The object I'm trying to stringify is this (I logged it in javascript console):

我试图字符串化的对象是这个(我在 javascript 控制台中记录了它):

Object
GsearchResultClass: "GlocalSearch"
accuracy: "8"
addressLines: Array[2]
city: "Cupertino"
content: ""
country: "United States"
ddUrl: "http://www.google.com/maps?source=uds&daddr=10825+North+Wolfe+Road,+Cupertino,+CA+(Southland+Flavor+…"
ddUrlFromHere: "http://www.google.com/maps?source=uds&saddr=10825+North+Wolfe+Road,+Cupertino,+CA+(Southland+Flavor+…"
ddUrlToHere: "http://www.google.com/maps?source=uds&daddr=10825+North+Wolfe+Road,+Cupertino,+CA+(Southland+Flavor+…"
html: HTMLDivElement
lat: "37.335405"
listingType: "local"
lng: "-122.015386"
maxAge: 604800
phoneNumbers: Array[1]
region: "CA"
staticMapUrl: "http://maps.google.com/maps/api/staticmap?maptype=roadmap&format=gif&sensor=false&size=150x100&zoom=…"
streetAddress: "10825 North Wolfe Road"
title: "Southland Flavor Cafe"
titleNoFormatting: "Southland Flavor Cafe"
url: "http://www.google.com/maps/place?source=uds&q=stinky&cid=9384294304761453216"
viewportmode: "computed"
__proto__: Object

And I'm doing it like this:

我是这样做的:

JSON.stringify(theobject);

回答by nss

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.

一个对象在某处引用自己;因此消息“循环结构”。我怀疑它可能在HTMLDivElement. 您是仅将它用于调试目的还是您真的想用这个 JSON 做一些有意义的事情?如果您只是将它用于调试,那么大多数现代 JavaScript 调试器都会让您将一个对象记录到控制台。如果你真的想用数据做一些事情,你应该只从这个对象中取出你需要的东西,然后把它们放到一个新的修剪过的对象中,你可以传递给JSON.stringify. 这个对象看起来像是来自 Google API 并且里面有很多额外的数据。

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.

如果您不介意破坏性地修改对象,请尝试有选择地清除可疑字段并查看 JSON.stringify 是否会接受该对象。至少这样你就会知道是什么导致了它。请注意,如果您这样做,最终可能会破坏对象以供将来使用。

回答by John Paul Barbagallo

Had this same problem, turns out I was an idiot and forgot the .val() at the end of the element that I wanted to insert into the JSON object.

遇到了同样的问题,结果我是个白痴,忘记了要插入到 JSON 对象中的元素末尾的 .val() 。

Be sure you're not shoving a whole element into the JSON object, otherwise it will attempt to JSON encode something that is not valid JSON.

确保您没有将整个元素推入 JSON 对象,否则它会尝试对无效的 JSON 内容进行 JSON 编码。

回答by radupb

I would look at the following

我会看以下内容

html: HTMLDivElementshould probably take the html of the element and not the element itself

html:HTMLDivElement应该采用元素的 html 而不是元素本身

or the following

或以下

Object -> proto-> Object -> proto-> Object -> ........

对象 ->原型-> 对象 ->原型-> 对象 -> ........