JsonArray: There is a cycle in the hierarchy! 问题解决

问题:
List<Resource> 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<Role> roles = new HashSet<Role>();Resource和Role有多对多的关系Role的属性       

private Integer id; private String roleName;

private Set<Resource> res = new HashSet<Resource>();
解决:
List<Resource> roomList=roomService.findByHotelsid(hotelsid);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(new net.sf.json.util.PropertyFilter(){
            @Override
            public boolean apply(Object source, String name, Object value) {
                if(name.equals("Roles")){
                    return true;
                }else{
                return false;
                }
            }});
       
        JSONArray json=JSONArray.fromObject(roomList,jsonConfig);

 

你可能感兴趣的:(json)