在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常。

意思是出现了死循环,也就是Model之间有循环包含关系;

解决办法:

使用setCycleDetectionStrategy防止自包含

代码:

  1. JsonConfig jsonConfig = new JsonConfig();
  2.  
  3. jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
  4.  
  5. JSONObject json =JSONObject.fromObject(model, jsonConfig);
  6.  
  7. result = json.toString();

依赖的包:

  1. import net.sf.json.JSONArray;
  2. import net.sf.json.JSONObject;
  3. import net.sf.json.JsonConfig;
  4. import net.sf.json.util.CycleDetectionStrategy;

完整实列

  1. /**
  2. * 这里测试如果含有自包含的时候需要CycleDetectionStrategy
  3. */
  4. public static void testCycleObject() {
  5. CycleObject object = new CycleObject();
  6. object.setMemberId("yajuntest");
  7. object.setSex("male");
  8. JsonConfig jsonConfig = new JsonConfig();
  9. jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
  10.  
  11. JSONObject json = JSONObject.fromObject(object, jsonConfig);
  12. System.out.println(json);
  13. }
  14.  
  15. public static void main(String[] args) {
  16. JsonTest.testCycleObject();
  17. }

其中 CycleObject.java

  1. public class CycleObject {
  2.  
  3. private String memberId;
  4. private String sex;
  5. private CycleObject me = this;
  6. …… // getters && setters
  7. }

json:There is a cycle in the hierarchy!的更多相关文章

  1. 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跟个,,要不个哪的对象系列 ...

  2. net.sf.json.JSONException: There is a cycle in the hierarchy!

    因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...

  3. net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案

    net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案 今天在用List集合转换成json数组的时候发生了这个错误,这个 ...

  4. 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法

    在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常.   意思是出现了死循环,也就是Model之间有循环包含关系: ...

  5. hibernate:There is a cycle in the hierarchy! 造成死循环解决办法

    下面是报的异常:在网上搜了关于:There is a cycle in the hierarchy!,才知道原来是因为死循环造成的!解决了好久,没有成功,后台不得已请教老大,老大说是因为在使用JSON ...

  6. There is a cycle in the hierarchy解决

    前言: 在一次项目中,分页查询公告列表信息后,在遍历查询到的公告列表时出现了死循环“There is a cycle in the hierarchy”错误,分析原因是因为在公告实体类中包含了商铺对象 ...

  7. There is a cycle in the hierarchy! role对象此时是什么错误

    There is a cycle in the hierarchy! role对象此时是什么错误

  8. net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法

    使用Hibernate manytoone属性关联主表的时候,如果使用JSONArray把pojo对象转换成json对象时,很容易出现循环的异常.解决的办法就是, 在转换json对象时忽略manyto ...

  9. json-lib-2.4-jdk15.jar 报错 net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案(Hibernate)

    使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决 ...

随机推荐

  1. 使用mx:Repeater在删除和添加item时列表闪烁

    使用mx:Repeater在删除和添加item时列表闪烁 不可能在用户界面上闪闪的吧,recycleChildren属性可帮助我们 recycleChildren属性==缓存,设为true就可以了 本 ...

  2. Meet python: little notes 4 - high-level characteristics

    Source: http://www.liaoxuefeng.com/ ♥ Slice Obtaining elements within required range from list or tu ...

  3. 解析百度搜索结果链接的url,获取真正的url

    通常,在百度输入关键词搜索出现的列表页,点击目标链接,然而跳转的时候却是百度地址,经过百度解析,才真的跳到目标页面. 在SEO中,经常需要看下自己的网站排名,又不想手动每天手动去点,可用以下方法去得到 ...

  4. [转]Spring JdbcTemplate 查询分页

    原文:http://blog.csdn.net/xiaofanku/article/details/4280128 现在进行的项目由于数据库的遗留原因(设计的不堪入目)不能用hibernate.所以用 ...

  5. grains

    用途 1,匹配客户端 2,配置文件里使用 3,资产管理     定义grains方法1:             方法2:        

  6. linux查看出口ip 及w3m字符浏览器

    Linux 查看服务器出口IP    字符浏览器: http://wiki.ubuntu.org.cn/W3m

  7. php数组函数,字符串,linux命令

    1>> Linux常用命令一. 文件目录操作命令1. ls命令    命令格式:ls [选项] [目录名]    命令功能:列出目标目录中所有的子目录和文件.2. 命令格式:cd [目录名 ...

  8. ssh 免密码设置失败原因总结

    先复习一下设置ssh免密码操作的步骤: 进入主目录 cd 生成公钥 ssh-keygen -t rsa -P '' (注:最后是二个单引号,表示不设置密码) 然后分发公钥到目标机器 ssh-copy- ...

  9. Python2.7-异常和工具

    来自<python学习手册第四版>第七部分,而且本书发布的时候3.1还未发布,所以针对本书的一些知识会有些滞后于python的版本,具体更多细节可以参考python的标准手册. 一.异常基 ...

  10. php json 格式控制

    本文同步至我的个人博客:http://www.52cik.com/2015/12/23/php-json-format.html 关于 json 这个问题,陆陆续续有新手朋友找我问,比如为什么我输出的 ...