JSON.stringify出现 "Converting circular structure to JSON"
JSON.stringify() 我们很熟悉了,将一个对象转换为json形式的字符串.
但是如果你在浏览器控制台中输出 JSON.stringify(window). 如果期望输出一段文字, 可能会失望了. 事实上, 会输出结果如下:
错误信息很明显了, 对象中有循环引用. 解决方案如下:
参考链接:http://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json
// Demo: Circular reference
var o = {};
o.o = o;
// Note: cache should not be re-used by repeated calls to JSON.stringify.
var cache = [];
JSON.stringify(o, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = null; // Enable garbage collection
JSON.stringify说明 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
至于出现循环引用的原因,参考如下:
原文链接:
http://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json
JSON.stringify出现 "Converting circular structure to JSON"的更多相关文章
- Object.stringify 循环引用 bug & TypeError: Converting circular structure to JSON
Object.stringify 循环引用 bug & TypeError: Converting circular structure to JSON var obj = { a: &quo ...
- Javascript报错Converting circular structure to JSON 错误排解
在运行nodejs程序的时候报出以下的错误: 2017-11-20 17:44 +08:00: TypeError: Converting circular structure to JSON at ...
- 项目中遇到Uncaught TypeError: Converting circular structure to JSON报错问题
最近公司项目中出现一个报错Uncaught TypeError: Converting circular structure to JSON,,根据上述报错可以知道代码是运行到JSON.stringi ...
- JSON.stringify方法报错:Converting circular structure to JSON
别以为JSON.parse(JSON.stringify(data))做深拷贝无敌,对于以下这种情况,当你需要保留父级对象,即 对象存在循环引用,就会报错. var a = [ { "id& ...
- 处理TypeError: Converting circular structure to JSON
// Demo: Circular reference var o = {}; o.o = o; // Note: cache should not be re-used by repeated ca ...
- Javascript报错Converting circular structure to JSON
主要是因为对象的互相引用,怎么样才能造成对象的互相引用呢? var a = {}; var b = {}; a.b = b; b.a = a; 怎么解决,反正我试了很多,最后选择深度clone thi ...
- 浅谈JSON.stringify 函数与toJosn函数和Json.parse函数
JSON.stringify 函数 (JavaScript) 语法:JSON.stringify(value [, replacer] [, space]) 将 JavaScript 值转换为 Jav ...
- js数组使用JSON.stringify()和toString()的区别,JSON.parse
JSON.stringify()中的<br><br>var arr = [1,2,3,4]; console.log(arr.toString()); // 1,2,3,4 a ...
- javascript 中的JSON.stringify - 将对象和数组转换为json格式(来源于网络)
JSON.stringify 函数 (JavaScript) 将 JavaScript 值转换为 JavaScript 对象表示法 (Json) 字符串. JSON.stringi ...
随机推荐
- 2017.2.20 activiti实战--第一章--认识Activiti
学习资料:<Activiti实战> 第一章 认识Activiti 内容概览:讲解activiti的特点.接口概览.架构等基本信息. 1.3 Activiti的特点 1.使用mybatis ...
- unity游戏开发
第1章 基础知识 11.1 Unity简介 11.2 跨平台与多工种协作 11.3 Unity版本 21.4 Unity内置资源或拓展资源 31.5 示例项目打包与发布 51.6 Unity服务 71 ...
- mysql 远程登陆不上
当使用 TCP/IP 连接 mysql 时, 出现 : Can't connect to MySQL server on 'xxx.xxx.xxx.xxx.'(111) 这个错误. 经过重复折腾: 确 ...
- 怎样备份Github博客至GitCafe
原文链接:http://stackvoid.com/how-to-transfer-github-pages-to-gitcafe/ 开通博客半年多了,一直将博客托管到 Github 上,使用 Git ...
- 成功者的特点 VS 失败者的特点
- 0x…… is not a valid instance ID怎么解决
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php猴子吃桃
<?php header("content-type:text/html;charset=utf-8"); /* 有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个! 以后 ...
- #測试相关#Getting “junit.framework.AssertionFailedError: Forked Java VM exited abnormally” Exception
编写Ant脚本进行持续測试的时候.出现了junit.framework.AssertionFailedError: Forked Java VM exited abnormally的报错,以此为key ...
- JS门面模式
门面模式 前言 门面模式的本质是实现一个简单的同一接口来处理对各个子系统接口的处理和调用.和桥接模式不同的是:桥接模式中的各个类是全然独立的,桥接模式仅仅在必要的时候将这些类关联起来. 门面模式则有点 ...
- Ubuntu搭建Http服务器
一句命令: sudo apt-get install apache2 产生的启动和停止文件是:/etc/init.d/apache2 启动:sudo apache2ctl -k start 停止:su ...