规避错误在执行JSON.stringify()时将循环结构转换为JSON? [英] circumvent ERROR Converting circular structure to JSON when doing JSON.stringify()?

查看:504
本文介绍了规避错误在执行JSON.stringify()时将循环结构转换为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于调试,我想用JSON.stringify(myobject)序列化javascript对象。但这给出了:

For debugging I want to serialize javascript objects with JSON.stringify(myobject). But this gives:

TypeError: Converting circular structure to JSON

有没有办法防止这种情况,例如修剪输出树?

Is there a way to prevent this, by for instance pruning the output tree?

更多背景:

我想收集关于不同对象的一些数据,看看发生了什么,以及为什么一个功能适用于一种情况但不适用于另一种情况。通过比较输出,我希望能够找到差异,这解释了为什么它不在另一种情况下工作。
我正在使用jquery,我的调试马叫做chrome。如果有更好的替代方法来进行这种类型的调试活动,我也非常感兴趣!

I want to collect some data on different objects and see what is going on, and why a feature works for one situation but not for another. By comparing outputs I hope to be able to find differences which explains why it is not working in "another" situation. I'm using jquery and my debug horse is called chrome. If there are better alternatives for doing this type of debugging activities I'm also very much interested!

干杯,
jeroen。

Cheers, jeroen.

推荐答案

JSON.stringify(obj)不支持循环引用,例如:

JSON.stringify(obj) does not support circular referencing such as:

var car = {}
car.myself = car;
JSON.stringify(car);

然而 dojox.json.ref 确实支持循环引用,如果你想探索另一个选项。

However dojox.json.ref does support circular referencing, if you wanted to explore another option.

但是,如果您的目的是严格调试,我建议使用内置的浏览器调试器,如Chrome,IE或Firebug(对于Firefox)。

However if your purposes are strictly to debug, I'd suggest using the built in browser debugger such as Chrome's, IE's or Firebug(for firefox).

这篇关于规避错误在执行JSON.stringify()时将循环结构转换为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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