Tag: stringify

JSON.stringify输出到div以漂亮的打印方式

我通过JSON.stringify一个json对象 result = JSON.stringify(message, my_json, 2) 在上面的参数2应该相当打印结果。 它做到这一点,如果我做一些像alert(result) 。 不过,我想通过将其附加到一个div内输出给用户。 当我这样做时,我只能看到一行。 (我不认为这是工作,因为rest和空间不被解释为HTML?) { "data": { "x": "1", "y": "1", "url": "http://url.com" }, "event": "start", "show": 1, "id": 50 } 有没有办法将JSON.stringify的结果以漂亮的打印方式输出到div?

如何将事件对象串起来

JSON.stringify(eventObject); 得到: TypeError: Converting circular structure to JSON dojox.json.ref.toJson(eventObject); 得到: TypeError: Accessing selectionEnd on an input element that cannot have a selection. 是否有一些库/代码可以用来完成它?

JSON.stringify深层对象

我需要一个函数从任何参数构build一个JSON有效的string,但是: 通过不添加对象两次来避免recursion性问题 通过截断给定的深度来避免调用堆栈大小问题 一般来说,它应该能够处理大对象,代价是截断它们。 作为参考,此代码失败: var json = JSON.stringify(window); 避免recursion问题很简单: var seen = []; return JSON.stringify(o, function(_, value) { if (typeof value === 'object' && value !== null) { if (seen.indexOf(value) !== -1) return; else seen.push(value); } return value; }); 但是现在,除了复制和更改道格拉斯·克罗克福德的代码来跟踪深度之外,我没有find任何方法来避免堆栈溢出,如window或任何event非常深的对象。 有一个简单的解决scheme吗?

将具有循环引用的JavaScript对象string化(转换为JSON)

我有一个包含循环引用的JavaScript对象定义:它有一个引用父对象的属性。 它也有我不想传递给服务器的function。 我将如何序列化和反序列化这些对象? 我读过最好的方法是使用Douglas Crockford的stringify。 不过,我在Chrome中收到以下错误: TypeError: Converting circular structure to JSON 代码: function finger(xid, xparent){ this.id = xid; this.xparent; //other attributes } function arm(xid, xparent){ this.id = xid; this.parent = xparent; this.fingers = []; //other attributes this.moveArm = function() { //moveArm function details – not included in this testcase alert("moveArm Executed"); } } function […]

序列化包含循环对象值的对象

我有一个对象(parsing树),其中包含引用其他节点的子节点。 我想序列化这个对象,使用JSON.stringify() ,但我得到: TypeError: cyclic object value因为我提到的构造。 我怎么能解决这个问题? 对于其他节点的这些引用是否在序列化对象中表示或者不在序列化对象中并不重要。 另一方面,从对象中删除这些属性时,他们正在创build似乎乏味,我不想修改parsing器(水仙)。