`

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

阅读更多
//设置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;
			}
			
		});
		//在转换的时候加个config
		JSONArray jsarr = JSONArray.fromObject(streetList,config);
		request.setAttribute("jsarr", jsarr);
		}


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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics