json:There is a cycle in the hierarchy!
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常。
意思是出现了死循环,也就是Model之间有循环包含关系;
解决办法:
使用setCycleDetectionStrategy防止自包含
代码:
- JsonConfig jsonConfig = new JsonConfig();
- jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
- JSONObject json =JSONObject.fromObject(model, jsonConfig);
- result = json.toString();
依赖的包:
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import net.sf.json.JsonConfig;
- import net.sf.json.util.CycleDetectionStrategy;
完整实列
- /**
- * 这里测试如果含有自包含的时候需要CycleDetectionStrategy
- */
- public static void testCycleObject() {
- CycleObject object = new CycleObject();
- object.setMemberId("yajuntest");
- object.setSex("male");
- JsonConfig jsonConfig = new JsonConfig();
- jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
- JSONObject json = JSONObject.fromObject(object, jsonConfig);
- System.out.println(json);
- }
- public static void main(String[] args) {
- JsonTest.testCycleObject();
- }
其中 CycleObject.java
- public class CycleObject {
- private String memberId;
- private String sex;
- private CycleObject me = this;
- …… // getters && setters
- }
json:There is a cycle in the hierarchy!的更多相关文章
- atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy
atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy 1. 环境:使用hibernate4跟个,,要不个哪的对象系列 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!
因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案 今天在用List集合转换成json数组的时候发生了这个错误,这个 ...
- 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常. 意思是出现了死循环,也就是Model之间有循环包含关系: ...
- hibernate:There is a cycle in the hierarchy! 造成死循环解决办法
下面是报的异常:在网上搜了关于:There is a cycle in the hierarchy!,才知道原来是因为死循环造成的!解决了好久,没有成功,后台不得已请教老大,老大说是因为在使用JSON ...
- There is a cycle in the hierarchy解决
前言: 在一次项目中,分页查询公告列表信息后,在遍历查询到的公告列表时出现了死循环“There is a cycle in the hierarchy”错误,分析原因是因为在公告实体类中包含了商铺对象 ...
- There is a cycle in the hierarchy! role对象此时是什么错误
There is a cycle in the hierarchy! role对象此时是什么错误
- net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法
使用Hibernate manytoone属性关联主表的时候,如果使用JSONArray把pojo对象转换成json对象时,很容易出现循环的异常.解决的办法就是, 在转换json对象时忽略manyto ...
- json-lib-2.4-jdk15.jar 报错 net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案(Hibernate)
使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决 ...
随机推荐
- 使用mx:Repeater在删除和添加item时列表闪烁
使用mx:Repeater在删除和添加item时列表闪烁 不可能在用户界面上闪闪的吧,recycleChildren属性可帮助我们 recycleChildren属性==缓存,设为true就可以了 本 ...
- Meet python: little notes 4 - high-level characteristics
Source: http://www.liaoxuefeng.com/ ♥ Slice Obtaining elements within required range from list or tu ...
- 解析百度搜索结果链接的url,获取真正的url
通常,在百度输入关键词搜索出现的列表页,点击目标链接,然而跳转的时候却是百度地址,经过百度解析,才真的跳到目标页面. 在SEO中,经常需要看下自己的网站排名,又不想手动每天手动去点,可用以下方法去得到 ...
- [转]Spring JdbcTemplate 查询分页
原文:http://blog.csdn.net/xiaofanku/article/details/4280128 现在进行的项目由于数据库的遗留原因(设计的不堪入目)不能用hibernate.所以用 ...
- grains
用途 1,匹配客户端 2,配置文件里使用 3,资产管理 定义grains方法1: 方法2:
- linux查看出口ip 及w3m字符浏览器
Linux 查看服务器出口IP 字符浏览器: http://wiki.ubuntu.org.cn/W3m
- php数组函数,字符串,linux命令
1>> Linux常用命令一. 文件目录操作命令1. ls命令 命令格式:ls [选项] [目录名] 命令功能:列出目标目录中所有的子目录和文件.2. 命令格式:cd [目录名 ...
- ssh 免密码设置失败原因总结
先复习一下设置ssh免密码操作的步骤: 进入主目录 cd 生成公钥 ssh-keygen -t rsa -P '' (注:最后是二个单引号,表示不设置密码) 然后分发公钥到目标机器 ssh-copy- ...
- Python2.7-异常和工具
来自<python学习手册第四版>第七部分,而且本书发布的时候3.1还未发布,所以针对本书的一些知识会有些滞后于python的版本,具体更多细节可以参考python的标准手册. 一.异常基 ...
- php json 格式控制
本文同步至我的个人博客:http://www.52cik.com/2015/12/23/php-json-format.html 关于 json 这个问题,陆陆续续有新手朋友找我问,比如为什么我输出的 ...