There is a cycle in the hierarchy解决办法:
错误原因:将对象转换为json串时的错误,可能原因是存在级联(及树结构)或者对象间的互相调用,进入了死循环,有些项目会经常遇到;
解决方法:去掉级联或者互相调用而导致干扰的对象,例如:
public void sendUser(User user) {
JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
jsonConfig.setExcludes(new String[]{"company","role","office","roleList","vacation","moreWorkTime"}); //此处是亮点,只要将所需忽略字段加到数组中即可
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
JSONObject json = JSONObject.fromObject(user,jsonConfig);
String jsonString = json.toString();
}