惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
爱范儿
爱范儿
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
Latest news
Latest news
The Hacker News
The Hacker News
Cyberwarzone
Cyberwarzone
博客园 - 【当耐特】
Project Zero
Project Zero
小众软件
小众软件
T
Tailwind CSS Blog
量子位
博客园 - 聂微东
I
Intezer
美团技术团队
S
SegmentFault 最新的问题
T
Tor Project blog
Spread Privacy
Spread Privacy
V
Vulnerabilities – Threatpost
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Jina AI
Jina AI
罗磊的独立博客
B
Blog RSS Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
C
Cisco Blogs
L
LINUX DO - 热门话题
Last Week in AI
Last Week in AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AI
AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Azure Blog
Microsoft Azure Blog
L
LINUX DO - 最新话题
Know Your Adversary
Know Your Adversary
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
The Register - Security
The Register - Security
L
LangChain Blog
博客园 - 叶小钗
T
Tenable Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - yueue

Ext Portal如何消除横向滚动条 JavaScript中的对象动态加载技术 JavaScript使用ACTIVEX控件引起崩溃问题的解决 如何把Ext.data.store里的数据一次性用JSON传给后台(添加了后台解析部分) 解决EXT DateField 用getValue() 发送到后台,后台取null的问题 - yueue 如何使开启了分页功能的EXT Combox可以自动选中非第一页的值 ExtJs 中一个Toolbar 不够用?再来一个! - yueue 为什么ext combox 下拉框不出现自动提示,自动选中 如何做一个两列的FormPanel EXT如何把一个对象转换为JSON并将其发送到服务器 - yueue - 博客园 ExtJs如何通过JSON与后台通信 ExtJs 及 Ajax 乱码解决方案 - yueue [教程]创建ADOKeycap数据库对象 认识 yueue.ADOKeycap 开源数据库组件 [教程]使用yueueData统计数据 [教程]在ADOKeycap中使用DataReader读取数据 [教程]使用ADOKeycap插入,更新,删除数据 [教程]使用AODKeycap读取数据 [教程]添加yueue.ADOKeycap数据库组件到您的项目 - yueue - 博客园
json-lib出现There is a cycle in the hierarchy解决办法
yueue · 2010-02-23 · via 博客园 - yueue

问题的出现

如果需要解析的数据间存在级联关系,而互相嵌套引用,在hibernate中极容易嵌套而抛出net.sf.json.JSONException: There is a cycle in the hierarchy异常。

解决办法

1.设置JSON-LIB让其过滤掉引起循环的字段。

Java代码 复制代码

  1. JsonConfig config = new JsonConfig();   
  2. config.setIgnoreDefaultExcludes(false);      
  3. config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);    
  4. config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register   
  5. config.setExcludes(new String[]{//只要设置这个数组,指定过滤哪些字段。   
  6.   "consignee",   
  7.   "contract",   
  8.   "coalInfo",   
  9.   "coalType",   
  10.   "startStation",   
  11.   "balanceMan",   
  12.   "endStation"  
  13. });   
  14. String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}";   
  15. out.print(tempStr);  
  JsonConfig config = new JsonConfig();  config.setIgnoreDefaultExcludes(false);     config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);   config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register  config.setExcludes(new String[]{//只要设置这个数组,指定过滤哪些字段。    "consignee",    "contract",    "coalInfo",    "coalType",    "startStation",    "balanceMan",    "endStation"  });  String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}";  out.print(tempStr);
 

2.设置JSON-LIB的setCycleDetectionStrategy属性让其自己处理循环,省事但是数据过于复杂的话会引起数据溢出或者效率低下。

Java代码 复制代码

  1. JsonConfig config = new JsonConfig();   
  2. config.setIgnoreDefaultExcludes(false);      
  3. config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);    
  4. config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register   
  5. String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}";   
  6. out.print(tempStr);