There is a circle错误,json转换

来源:互联网 发布:控制上网的软件 编辑:程序博客网 时间:2024/06/04 18:54

在做ssh项目中,我们经常会用到ajax,服务端我们经常会返回一个json的对象,但是经常会出现这个There is a circle的错误。

错误的原因是因为hibernate中的外键关联另一个对象而另一个对象也同时关联了这个对象,造成的死循环。解决方法:

 

 

HttpServletResponse response = ServletActionContext.getResponse();response.setCharacterEncoding("utf-8");if("ajax".equals(mark)){JsonConfig config = new JsonConfig();//解决there is a cycle 问题config.setExcludes(new String[]{"empUsername","empPwd","empRemark","empAddress","empPhone","empPicturepath","empSex","manualsigns","documents","department","role","position","operationlogs","notes","messagesForSendEmp","schedules","messagesForReceiveEmp",""});JSONArray jarry = JSONArray.fromObject(employees,config);response.getWriter().write(jarry.toString());System.out.println(jarry);return null;}else{for (Employee item : employees) {employeesMap.put(item.getEmpId(),item.getEmpName());}}return "search_success";

例如,这个例子中,我只想返回一个id和一个name,但是这个对象里面拥有很多其他无关的属性包括外键对象,这个时候我们可以利用config.setExcludes();这个方法,过滤掉那些不需要的属性,然后得到的json对象就只有我们需要的这两个属性了。