将JSON转换为字符串时,如何解决此Javascript错误? [英] How do I solve this Javascript error when converting JSON to string?

查看:103
本文介绍了将JSON转换为字符串时,如何解决此Javascript错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Uncaught TypeError: Converting circular structure to JSON

我要分类的对象是这个(我在javascript控制台中记录了它):

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

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

我正在这样做:

JSON.stringify(theobject);

推荐答案

一个对象在某处引用了自己.因此,消息为圆形结构".我怀疑它可能在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.

这篇关于将JSON转换为字符串时,如何解决此Javascript错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆