solve this Javascript error when converting JSON to string

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

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

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,沒錯,這個錯誤就是指針循環了致使的。java

由於從select2中或得的對象裏面包含了大量select2的api信息,有的信息存在指針循環的現象,能夠經過網上一些人的辦法經過替換這樣問題的對象,也能夠採用個人辦法,就是將本身須要的信息提取出來,一目瞭然,不會再發生前面提到的錯誤。json

用stackoverflow上面最佳答案就是: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.數組

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.this

以上google

Thank you!
相關文章
相關標籤/搜索