zoukankan      html  css  js  c++  java
  • json:There is a cycle in the hierarchy!

    在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常。

    意思是出现了死循环,也就是Model之间有循环包含关系;

    解决办法:

    使用setCycleDetectionStrategy防止自包含

    代码:

    JsonConfig jsonConfig = new JsonConfig();
    
    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
    
    JSONObject json =JSONObject.fromObject(model, jsonConfig);
    
    result = json.toString();

    依赖的包: 

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import net.sf.json.JsonConfig;
    import net.sf.json.util.CycleDetectionStrategy;

    完整实列

    /** 
        * 这里测试如果含有自包含的时候需要CycleDetectionStrategy 
        */  
       public static void testCycleObject() {  
           CycleObject object = new CycleObject();  
           object.setMemberId("yajuntest");  
           object.setSex("male");  
           JsonConfig jsonConfig = new JsonConfig();  
           jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);  
      
           JSONObject json = JSONObject.fromObject(object, jsonConfig);  
           System.out.println(json);  
       }  
      
       public static void main(String[] args) {  
                  JsonTest.testCycleObject();  
       }  

    其中 CycleObject.java

    public class CycleObject {  
      
        private String      memberId;  
        private String      sex;  
        private CycleObject me = this;  
    …… // getters && setters  
    } 
  • 相关阅读:
    element-ui获取table行数据
    去掉输入框的边框以及在显示获取焦点时的边框+jq日期选择器
    需要ui的小伙伴看过来(这篇博客只有一个链接希望对大家有用)
    vue获取当前对象
    FlashFXP用到的功能
    VS Code做项目的笔记
    单点登陆
    idea中自动生成实体类
    VSCode安装
    数组排序
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/4667832.html
Copyright © 2011-2022 走看看