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); ...
随机推荐
- 通过yum安装maven
安装maven wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/y ...
- 运维ipvsadm配置负载均衡
一.负载均衡LVS基本介绍 LB集群的架构和原理很简单,就是当用户的请求过来时,会直接分发到Director Server上,然后它把用户的请求根据设置好的调度算法,智能均衡地分发到后端真正服务器(r ...
- vue 中 @click.native.prevent 事件
在项目中看到@click.native.prevent, 查了一点资料 总结一下, 1.给vue组件绑定事件时候,必须加上native ,否则会认为监听的是来自Item组件自定义的事件, 2.prev ...
- Git 操作 GitHub
Git安装 https://www.cnblogs.com/taopanfeng/p/11076702.html 设置用户名(设置一次 以后就不用再设置了) git config --global u ...
- redis windows相关操作笔记
设置远程访问 1.注释掉bind 127.0.0.1. 2.protected-mode属性从yes改为no. 启动redis服务:redis-server.exe redis.windows.con ...
- 第03课:GDB常用的调试命令概览
先给出一个常用命令的列表,后面结合具体的例子详细介绍每个命令的用法. 命令名称 命令缩写 命令说明 run r 运行一个程序 continue c 让暂停的程序继续运行 next n 运行到下 ...
- xavier_uniform/xavier_normal
import math from torch.autograd import Variable import torch import torch.nn as nn import warnings w ...
- 深入理解JAVA虚拟机 高效并发
处理器和缓存 由于计算机的存储设备与处理器的运算速度之间有着几个数量级的差距,所以现代计算机系统都不得不加入一层读写速度尽可能接近处理器运算速度的高速缓存来作为内存与处理之间的缓冲:将运算需要使用的数 ...
- Luogu P3886 [JLOI2009]神秘的生物 最小表示法,轮廓线DP,插头DP,动态规划
亲手写掉的第一道最小表示法!哈哈哈太开心啦~ 不同于以往的几个插头\(dp\),这个题目的轮廓线是周围的一圈\(n\)个格子.而其所谓"插头"也变成了相邻格子的所属连通分量编号,并 ...
- 破解wifi_失败
sudo apt install reaver aircrack-ng //安装需要的软件包ifconfig //获取本地网卡接口sudo airmon-ng start wlp3s0 //无线网卡设 ...