json转换数据时候,报there is a cycle in the hierarchy!

来源:互联网 发布:磁力链接搜索引擎源码 编辑:程序博客网 时间:2024/05/16 13:01
//设置json过滤主外键,防止出现死循环there is a cycle in the hierarchy! JsonConfig config = new JsonConfig();config.setJsonPropertyFilter(new PropertyFilter(){//过滤属性//其中houses,district就是我们要过滤的字段,也就是JavaBean中的属性名public boolean apply(Object arg0, String name, Object arg2) {if(name.equals("houses")||name.equals("district")){return true;}return false;}});//在转换的时候加个configJSONArray jsarr = JSONArray.fromObject(streetList,config);request.setAttribute("jsarr", jsarr);}

这个异常,是由于,json在转换数据的时候,能够检测出要转换的属性之间存在着主外键关系,特别是hibernate在操作数据的时候,造成了死循环,解决办法,就是用JsonConfig提供的一个过滤属性的方法,实现里面的apply的方法,然后将要过滤的属性进行判断。这样就可以解决这个问题了。