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

来源:互联网 发布:淘宝链接转淘口令在线 编辑:程序博客网 时间:2024/06/11 05:22

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

使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系。比如说类别和商品,在商品表里面有类别属性,而在类别表里面有个商品集合,这样就存在了嵌套引用的问题了,就会抛出这个异常。

解决方法:解决方法很简单,在将每个对象转为json对象的时候用setExcludes函数将级联的属性去除掉就可以了,如下面:

public String list() throws Exception {// 1.调用service查询类别数据List<Category> list = categoryService.getCategoryList();System.out.println("666666666666"+list);// 2.将list转化为json数据JsonConfig jsonConfig=new JsonConfig();    jsonConfig.setExcludes(new String[]{"products"});//去除级联关系         String json=JSONArray.fromObject(list, jsonConfig).toString();// 3.将json发送给浏览器ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");ServletActionContext.getResponse().getWriter().write(json);return null;//告诉struts2不需要进行结果处理}

使用json取值一直都取不到,这个问题纠结了快一天,查了网上资料,对比自己遇到的问题,最后终于解决了,记录下来

阅读全文
0 0