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

This q come from stackoverflow see:
http://stackoverflow.com/questions/4954242/how-do-i-solve-this-javascript-error-when-converting-json-to-string

Uncaught TypeError: Converting circular structure to JSON
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
And I'm doing it like this:

JSON.stringify(theobject);
 这个问题已经提问者已经解决,虽然解决的办法不尽人意,但是他采纳了公众赞同的答案。
 
 作者也在这个问题上花了一部分时间,下面阐述一下我的解决方法。
 
  var v =$("#tagse").select2("data");

        //解决转换时指针循环问题
        var arr1 = [];
        for(var i=0;i<v.length;i++){
            var temparr = new Object();
            temparr.id = v[i].id;
            temparr.text = v[i].text;
            arr1[i] = temparr;
        }
        $("#tags").val(JSON.stringify(arr1));

这段代码大体背景就是从select2中获取到的结果其实是一个对象数组中嵌套对象数组。
但是通过JSON.stringify来讲对象转换成json有的时候回提示错误:Uncaught TypeError: Converting circular structure to JSON,没错,这个错误就是指针循环了导致的。

因为从select2中或得的对象里面包含了大量select2的api信息,有的信息存在指针循环的现象,可以通过网上一些人的办法通过替换这样问题的对象,也可以采用我的办法,就是将自己需要的信息提取出来,一目了然,不会再发生前面提到的错误。

用stackoverflow上面最佳答案就是:

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.

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.

以上

 Thank you!

伤痕小丑鱼
134 声望6 粉丝

技术宅改变世界!