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跟个,,要不个哪的对象系列化成个json的时候儿有这个问题了...
1

2. 原因::hb默认的lazy 方式造成的当有关联对象的时候儿...
1

3. #---解决::lazy =false  (推荐).. 1

4. 别的有以下的四个方法可以解决hibernate的序列化问题 2

5. BeanUtils.copyProperties  可以解决... 2

6. 属性过滤器PropertyFilter 2

7. 简单属性过滤器setExclusions法 3

8. JsonBeanProcessor法 4

9. 设置JSON-LIB的setCycleDetectionStrategy属性让其自己处理循环(ati测试不生效O81)..
4

10. 参考 5

1. 环境:使用hibernate4跟个,,要不个哪的对象系列化成个json的时候儿有这个问题了...

2. 原因::hb默认的lazy 方式造成的当有关联对象的时候儿...

3. #---解决::lazy =false  (推荐)..

<!-- o7o ati -->

<!-- many开头的是代表该表持有外键 -->

<!-- class可以不写,因为根据name的值computer(属性),会通过反射自动找到属于哪个类的 -->

<many-to-one name="prgrm"  insert="false" update="false"  lazy="false">

<column name="progarmme_id" />

</many-to-one>

作者:: 老哇的爪子 Attilax 艾龙,  EMAIL:[email protected]

转载请注明来源: http://blog.csdn.net/attilax

4. 别的有以下的四个方法可以解决hibernate的序列化问题

1 domain类实现JSONString接口

2 建立JsonConfig实例,并配置属性排除列表

3 用属性过滤器

4 写一个自定义的JsonBeanProcessor

5. BeanUtils.copyProperties  可以解决...

GvProgramme stp=(GvProgramme) arg1;

GvProgramme o=new GvProgramme();

BeanUtils.copyProperties(o, stp);

6. 属性过滤器PropertyFilter

// 先过滤对set集合的拆解 4                JsonConfig config = new JsonConfig(); 5                config.setJsonPropertyFilter(new PropertyFilter() { 6                    @Override 7                    public boolean apply(Object arg0, String arg1, Object arg2) { 8                        if (arg1.equals("shoppingCarts")) { 9                            return true;10                        } else {11                            return false;12                        }13                    }14                });15                // 将数据转换成Json数据16                JSONArray jsonObject = JSONArray.fromObject(listCarts, config);17                System.out.println(jsonObject.toString());

7. 简单属性过滤器setExclusions法

2.第二种方法通过jsonconfig实例,对包含和需要排除的属性进行方便添加删除

[java]view plaincopyprint?

5 public class Person {<br>

6    private String name;<br>

7    private String lastname;<br>

8    private Address address;<br>

9  <br>

10    // getters & setters<br>

11 }<br>

12  <br>

13 JsonConfig jsonConfig = new JsonConfig();<br>

14 jsonConfig.setExclusions( new String[]{ "address" } );<br>

15 Person bean = /* initialize */;<br>

16 JSON json = JSONSerializer.toJSON( bean, jsonConfig );

注意:这种方法不区分目标类,就是说如果有2个bean当中都存在“address”属性,那么采用这种方法,这两个bean中的address属 性都将被排除

3. 使用propertyFilter可以允许同时对需要排除的属性和类进行控制,这种控制还可以是双向的,也可以应用到json字符串到java对象

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

LibtypeDAO libtypeDAO = new LibtypeDAO();

List<Libtype> list = libtypeDAO.findAll();

JsonConfig jsonConfig = new JsonConfig();  //建立配置文件

jsonConfig.setIgnoreDefaultExcludes(false);  //设置默认忽略

jsonConfig.setExcludes(new String[]{"libs"});  //此处是亮点,只要将所需忽略字段加到数组中即可,在上述案例中,所要忽略的是“libs”,那么将其添到数组中即可,在实际测试中,我发现在所返回 数组中,存在大量无用属性,如“multipartRequestHandler”,“servletWrapper”,那么也可以将这两个加到忽略数组 中.

8. JsonBeanProcessor法

4. 最后来看JsonBeanProcessor,这种方式和实现JsonString很类似,返回一个代表原来的domain类的合法JSONOBJECT

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

9. 设置JSON-LIB的setCycleDetectionStrategy属性让其自己处理循环(ati测试不生效O81)..

2.

3.省事但是数据过于复杂的话会引起数据溢出或者效率低下。

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

LibtypeDAO libtypeDAO = new LibtypeDAO();

List<Libtype> list = libtypeDAO.findAll();

JsonConfig jsonConfig = new JsonConfig(); //建立配置文件

jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略

jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);   //此处是亮点,不过经过测试,第2种方法有些悲剧,虽然可以使用,但其结果貌似循环数次,至于为啥,还请高人指点。

JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig); //加载配置文件

return null;

}

3.

10. 参考

关于json-lib There is a cycle in the hierarchy!问题的3种解决办法_威廉庄园_williambryantliu的和讯博客.htm

使用json-lib完成json的序列化和反序列化 - 话别深秋 - 博客频道 - CSDN.NET.htm

hibernate中lazy的使用 - TOYOE - 博客园.htm

atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy

时间: 2024-08-05 18:20:12

atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy的相关文章

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

原因分析:在解析bean时,出现死循环调用,即:多个Bean之间出现了相互调用解决办法:过滤去掉bean中引起死循环调用的属性: [java] view plain copy List<Project> projectList = projectServices.find();  //获取数据 //自定义JsonConfig用于过滤Hibernate配置文件所产生的递归数据 JsonConfig config = new JsonConfig(); config.setExcludes(new

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

使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决方法很简单,在将每个对象转为json对象的时候用setExcludes函数将级联的属性去除掉就可以了,如下面: 比如CityVo  里面有关联属性 ProvinceVo province 以及 地区属性 Set<DistrictVo> districtList 使用hibernate 查询所有的c

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

怎么解决? 过滤掉集合属性便可 防止死循环

Json解析时出现net.sf.json.JSONException: There is a cycle in the hierarchy!

原因分析在解析bean时,出现死循环调用,即多个bean之间出现了相互调用.解决方法:将关联关系中实体对象间 的lazy属性设为false过滤掉bean中引起死循环调用的属性.(两种过滤方式) //采用数组的方式过滤关联的实体对象 JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setIgnoreDefaultExcludes(false); jsonConfig.setCycleDetectionStrategy(CycleDetect

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

使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决方法很简单,在将每个对象转为json对象的时候用setExcludes函数将级联的属性去除掉就可以了,如下面: //得到所有部门     //返回json对象字符串     public String getAllDep(){         List list = deptDAO.findAll(

JSONException: There is a cycle in the hierarchy解决

使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决方法很简单,在将每个对象转为json对象的时候用setExcludes函数将级联的属性去除掉就可以了,如下面: //得到所有部门 //返回json对象字符串 public String getAllDep(){ List list = deptDAO.findAll(); JsonConfig co

net.sf.json.JSONException: &#39;object&#39; is an array. Use JSONArray instead

list集合转换JSON出错误 意思是:对象"是一个数组.使用jsonarray代替. 解决方法: 将JSONObject替换为JSONArray 代码: JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSONArray json = JSONArray.fromObject(newList, jsonConfig);

net.sf.json.JSONException: &amp;#39;object&amp;#39; is an array. Use JSONArray instead

list集合转换JSON出错误 意思是:对象"是一个数组. 使用jsonarray取代. 解决方法: 将JSONObject替换为JSONArray 代码: JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSONArray json = JSONArray.fromObject(newList, jsonConfig);

JSON中 net.sf.json.JSONException: java.lang.NoSuchMethodException异常

在json对象和java对象转换时 String s = "{'name':'name1','pwd':'pwd1'}"; Person p = (Person)JSONObject.toBean(JSONObject.fromObject(s), Person.class); System.out.println(p.getPwd()); 上面代码中出现以下异常: net.sf.json.JSONException: java.lang.NoSuchMethodException [