JSON.stringify方法报错:Converting circular structure to JSON
别以为JSON.parse(JSON.stringify(data))做深拷贝无敌,对于以下这种情况,当你需要保留父级对象,即 对象存在循环引用,就会报错。
var a = [
{
"id":5,
"pid":2,
"categoryName":"搜索行为",
},
{
"id":6,
"pid":3,
"categoryName":"购买力",
}
]
a.map(item => {
item.parent = item
return item
})
let b = JSON.stringify(a)
console.log(b)
报错

正确的方法是:
var a = [
{
"id":5,
"pid":2,
"categoryName":"搜索行为",
},
{
"id":6,
"pid":3,
"categoryName":"购买力",
}
]
a.map(item => {
item.parent = JSON.parse(JSON.stringify(item)) // 注意这里
return item
})
let b = JSON.stringify(a)
console.log(b)
更精简的情况:
var a = {};
a.o = a;
console.log(JSON.stringify(o))
JSON.stringify方法报错:Converting circular structure to JSON的更多相关文章
- Javascript报错Converting circular structure to JSON 错误排解
在运行nodejs程序的时候报出以下的错误: 2017-11-20 17:44 +08:00: TypeError: Converting circular structure to JSON at ...
- Javascript报错Converting circular structure to JSON
主要是因为对象的互相引用,怎么样才能造成对象的互相引用呢? var a = {}; var b = {}; a.b = b; b.a = a; 怎么解决,反正我试了很多,最后选择深度clone thi ...
- 项目中遇到Uncaught TypeError: Converting circular structure to JSON报错问题
最近公司项目中出现一个报错Uncaught TypeError: Converting circular structure to JSON,,根据上述报错可以知道代码是运行到JSON.stringi ...
- Object.stringify 循环引用 bug & TypeError: Converting circular structure to JSON
Object.stringify 循环引用 bug & TypeError: Converting circular structure to JSON var obj = { a: &quo ...
- JSON.stringify出现 "Converting circular structure to JSON"
JSON.stringify() 我们很熟悉了,将一个对象转换为json形式的字符串. 但是如果你在浏览器控制台中输出 JSON.stringify(window). 如果期望输出一段文字, 可能会 ...
- 处理TypeError: Converting circular structure to JSON
// Demo: Circular reference var o = {}; o.o = o; // Note: cache should not be re-used by repeated ca ...
- Node.js中读取文件后用Json.parse方法报错
今天,在调试一个node项目时,发现了一个很大的坑,在此分享给大家! 大家都知道,Json.parse()方法对格式要求是很严格的,格式不对极其容易报错,但是有时候格式看似是正确的也会报错. 比如这一 ...
- Node.js中读取文件后用Json.parse方法报错解决方案
今天,在调试一个node项目时,发现了一个很大的坑,在此分享给大家! 大家都知道,Json.parse()方法对格式要求是很严格的,格式不对极其容易报错,但是有时候格式看似是正确的也会报错. 比如这一 ...
- JSON.stringify转化报错
两种方式会导致该错误:1.json格式数据存在循环调用. 举个例子: var obj = { title: '标题'}obj.content = obj;JSON.stringify(obj); ...
随机推荐
- ckfinder的使用
引入<script type="text/javascript" src="${ctxStatic}/ckfinder/ckfinder.js">& ...
- 阿里云云效平台使用——Windows上使用阿里云云效(RDC)Git拉取代码
转载:https://blog.csdn.net/for_my_life/article/details/88700696 SSH key配置 1.首先从开始菜单里面打开刚刚安装完成的Git目录下Gi ...
- 定义Vue-router的动态路由,获取传过来的动态参数
设置:在router目录下的index.js文件中,对path属性加上/:id 获取:使用router对象的params.id
- Json解析报错: Error is : Unescaped control character...的解决方法
在利用json-framework来实现json解析的过程时,会出现"-JSONValue Failed. Error is : Unescaped control character&qu ...
- 四、Vue CLI-异步请求(axios)
一.模块的安装 npm install axios --save #--save可以不用写 如图: 二.配置main.js import axios from 'axios' Vue.prototyp ...
- Codeforces Round #575 (Div. 3) C. Robot Breakout (模拟,实现)
C. Robot Breakout time limit per test3 seconds memory limit per test256 megabytes inputstandard inpu ...
- java知识
DiskFileUploadhttps://blog.csdn.net/FightingITPanda/article/details/79742631 import java.util.ArrayL ...
- 新建ext4分区及开机挂载
1.查看新的20G硬盘是否已经挂在完毕. 2.使用fdisk命令创建主分区 3.再将分区设置完毕之后,查看磁盘分区是否创建完成. 2.使用mkfs.ext4命令将新创建的分区进行格式化为: 1)blo ...
- html头部和底部固定时,中间的内容随屏幕分别率铺满页面
html页面头部和底部有东西时,怎么让内容填充到中间的页面,且去适应不同的电脑分辨率,看代码 <!DOCTYPE html> <html> <head> <m ...
- python类库32[多进程通信Queue+Pipe+Value+Array]
多进程通信 queue和pipe的区别: pipe用来在两个进程间通信.queue用来在多个进程间实现通信. 此两种方法为所有系统多进程通信的基本方法,几乎所有的语言都支持此两种方法. 1)Queue ...