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类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决 ...
随机推荐
- 关于OATUH中的AUTHRAZITON CODE和TOKEN的关系,实际上就是这么回事
关于OATUH中的AUTHRAZITON CODE和TOKEN的关系,实际上就是这么回事 每回要拿AUTHRAZITON CODE换取TOKEN,然后才能正常通信, 为什么要多一步呢?直接给TOKEN ...
- Java面向对象编程之异常处理机制
一:Java的异常处理机制的优点: 1:把各种不同情况的异常情况分类,使用JAVA类来表示异常情况,这种类被称为异常类.把各种异常情况表示成异常类,可以充分的发挥类的可扩展性和可重用性. 2:异常流程 ...
- [No000010]Ruby 中一些百分号(%)的用法小结
#Ruby 中一些百分号(%)的用法小结 #这篇文章主要介绍了Ruby 中一些百分号(%)的用法小结,需要的朋友可以参考下 what_frank_said = "Hello!"#% ...
- C# — FileHandler
学会使用OpenFileDialog和SaveFileDialog控件浏览和选择文件.使用System.IO.File和System.IO.Directory的对象来操纵文件系统(文件和目录). 在F ...
- webpack里的module选项配置
webpack里的module 有loaders选项和noParse选项,noParse选项配置不需要解析的目录和文件 module:{ loaders:[], noParse: [ path.joi ...
- sublime text2安装package control的方法
Package Control 方法一:在线安装,首先打开 Ctrl + ~,输入如下的代码: import urllib2,os; pf='Package Control.sublime-packa ...
- 表单事件onsubmit与onreset
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- linux 防火墙开启80端口永久保存
经常使用CentOS的朋友,可能会遇到和我一样的问题.开启了防火墙导致80端口无法访问,刚开始学习centos的朋友可以参考下.经常使用CentOS的朋友,可能会遇到和我一样的问题.最近在Linux ...
- javascript中的链表结构—从链表中删除元素
1.概念 上一个博文我们讲到链表,其中有一个方法remove()是暂时注释的,这个方法有点复杂,需要添加一个Previous()方法找到要删除的元素的前一个节点,这一个博文我们来分析一下这个remov ...
- php的一些小细节
1.今天看见 $arr3 = array_filter($arr, create_function('$v', 'return strlen($v);')); 作用就是去掉为空的元素,其实当callb ...