出现net.sf.json.JSONException: There is a cycle in the hierarchy异常的解决办法

简介:

因为项目中使用了AJAX技术,JAR包为:json-lib.jar,?在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题:

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


at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
at net.sf.json.JSONObject._fromBean(JSONObject.java:857)
at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
at net.yanhl.iouser.action.IOUserAction.loadUser(IOUserAction.java:142)


因为Hibernate中设置了自身关联: Iouser.hbm.xml:

//设置自身关联的组对象


public class GroupRelation implements Serializable {

private static final long serialVersionUID = 6202253180943473205L;
private Integer id;// 主键ID
private Integer creatorId;// 创建人
private Date createDate;// 创建日期
private String groupName;// 组名称
private GroupRelation parentGroup;
private Set childGroups = new HashSet();



起初想通过hibernate来解决问题,就是想过滤掉自身关联后来查资料发现不可能实现,最后找到通过JSON-LIB来过滤关联的集合属性,代码如下:


JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter(){
    public boolean apply(Object source, String name, Object value) {
        if(name.equals("parentGroup") || name.equals("childGroups")) {
            return true;
        } else {
            return false;
        }
    }
});
Iouser user = (Iouser) getBaseManager().get(Iouser.class, iouserId);
JSONObject jsonObject = JSONObject.fromObject(user, config);


当JSON-LIB解析JAVABEAN时过滤掉parentGroup、childGroups这两个属性,重新启动服务,pass

相关文章
|
5月前
|
JSON JavaScript 数据格式
vue-element-admin表格json数据渲染,异常数据一行显示红色
vue-element-admin表格json数据渲染,异常数据一行显示红色
65 1
|
2月前
|
SQL JSON Apache
Flink问题之嵌套 json 中string 数组的解析异常如何解决
Apache Flink是由Apache软件基金会开发的开源流处理框架,其核心是用Java和Scala编写的分布式流数据流引擎。本合集提供有关Apache Flink相关技术、使用技巧和最佳实践的资源。
219 1
|
4月前
|
JSON 数据格式
这个错误是由于在解析JSON数据时出现了问题。你可以尝试使用`try-except`语句来捕获异常
这个错误是由于在解析JSON数据时出现了问题。你可以尝试使用`try-except`语句来捕获异常
37 2
|
6月前
|
JSON Java 测试技术
[已解决]HttpMessageNotReadableException: JSON parse error: Unexpected character:解析JSON时出现异常的问题分析与解决方案
[已解决]HttpMessageNotReadableException: JSON parse error: Unexpected character:解析JSON时出现异常的问题分析与解决方案
169 0
|
8月前
|
JSON 前端开发 数据格式
2021-08-13service层校验异常处理器,拦截器,json转换器
2021-08-13service层校验异常处理器,拦截器,json转换器
43 0
|
11月前
|
JSON Android开发 数据格式
Android:解析Json异常 Expected a string but was BEGIN_OBJECT at
今天解析后端数据时,发现了这个报错:Expected BEGIN_OBJECT but was STRING at 看来是自己哪儿解析错误了。 因为数据的特殊性,后端返回的Json串里面可能还会有Json数据,可能嵌套了三次层,
|
JSON Java 关系型数据库
springboot基础入门之json转换框架 、全局异常捕捉以及JPA连接数据库
springboot基础入门之json转换框架 、全局异常捕捉以及JPA连接数据库
|
JSON JavaScript 数据格式
nodejs 异常退出解决&读取Json配置文件
nodejs 异常退出解决&读取Json配置文件
102 0
|
JSON Java 数据格式
Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json
参考文章: Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json 解决spring boot中rest接口404,500等错误返回统一的json格式 spring boot最新教程(五):404错误500错误统...
3078 0
Kam
|
JSON 数据格式
jackson json转实体异常记录
jackson json转实体异常记录
Kam
144 0