我有一個JavaScript對象我想要序列作爲字符串:類型錯誤在週期的原因透過JSON.stringify()時
key {...} Object
mandant "00001" String
personalNummer 600235 Number
首先我用JSON2其中返回值是undefined
。隨着JSON3我得到一個TypeError
並在json3.js
線路評論說:
// Cyclic structures cannot be serialized by `JSON.stringify`.
這個問題似乎從以下行導致json3.js:
// Manually invoke the callback for the `constructor` property due to
// cross-environment inconsistencies.
if (isConstructor || isProperty.call(object, (property = "constructor"))) {
callback(property);
}
但不應該有周期和我很明顯,他們無法知道究竟是怎麼回事。
當我在手工創建對象時調試一切正常。
那麼,什麼會引發錯誤?
編輯:
我成功準備一個場景產生錯誤:
- 它只是發生在IE9兼容性模式IE7和IE8(火狐22是好的,太)
- 剛剛發生,如果一個新窗口被打開,其中引用來自開啓器窗口的數據
* JSON_Cycle.html *:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://bestiejs.github.io/json3/lib/json3.js"></script>
<script>
var dataGlobal = {mandant: "Hallo Welt!", personalNummer: 123456};
$(function() {
window.open("JSON_Cycle_Popup.html", 'popup');
});
</script>
* JSON_Cycle_Popup.html *:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://bestiejs.github.io/json3/lib/json3.js"></script>
<script>
var dataGlobal = null;
$(function() {
dataGlobal = window.opener.dataGlobal;
alert(JSON.stringify(dataGlobal));
});
</script>
你能提供生成錯誤所需的最少代碼嗎? – user568109
我很喜歡,但是手工創建這樣的對象時不會顯示問題。似乎只有當對象通過AJAX($ .post())加載時纔會出現。在服務器端,我使用Jackson來序列化PoJO。我將設置一個方案試圖挑起錯誤... –
@MarvinEmilBrach鑑於數據是作爲JSON字符串從服務器返回的,爲什麼要將它轉換爲僅用於將其轉換回JSON字符串的對象再次? –