There is a cycle in the hierarchy! - 全栈笔记

There is a cycle in the hierarchy!

任亦伟 JAVA, 默认分类 2014-11-14 103 次浏览 没有评论

List res = resourceService.findAll();
JSONArray datalist = JSONArray.fromObject(res);//这里报错there is a cycle in the hierarchy

用网上提供的方法不好使
下面是Resource属性
private Integer id;
private String title;
private String description;
private Set roles = new HashSet();

Resource和Role有多对多的关系
Role的属性
private Integer id;
private String roleName;
private Set res = new HashSet();
有把有关联的属性放到json数组中就可以了。因为Resource的json数组中有roles数组,而且roles的数组元素中又有Resource数组,死循环了。用JSONConfig把roles去除就可以了

JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{“roles”});
JSONObject.fromObject(json, config);

来自:http://zhidao.baidu.com/link?url=Tf2eJhUhFebD2s9pNZt9cbOZBiqT8f1wf-2UBPjjQV1TXo-iG_jU2JHCykhejuMf5So_7Fs8BjQFo1fxkx_Vk_

通过实现接口来实现:

clientList = this.clientService.getListByPage((this.getPage() – 1)
* this.getRows(), this.getRows());

Map<String, Object> json = new HashMap<String, Object>();
json.put(“total”, 20);//total键 存放总记录数,必须的 json.put(“rows”, jsonArray);// rows键 存放每页记录 list (粗体字表示所有记录数据条数)
json.put(“rows”, clientList);

JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter() {

public boolean apply(Object source, String name, Object value) {
if(source instanceof Client){
if(“contacts”.equals(name) || “clientgroup”.equals(name) || “clientlevel”.equals(name)){
return true;
}else{
return false;
}
}
return false;
}
});
resultObj = JSONObject.fromObject(json,config);

发表评论

回顶部