【Other】关于There is a cycle in the hierarchy!

原因:

  父对象与子对象互相包含对方的类型的属性,造成死循环

解决方案1:

  JsonConfig config=new JsonConfig();

  config.setExcludes(new String[]{"devTask","employee","probClient"});  //传入要忽略的属性数组

  JSONObject json=JSONObject.fromObject(new Object(),config);    

 

解决方案2:

 

 

  

JsonConfig config=new JsonConfig();
  config.setJsonPropertyFilter(new PropertyFilter(){
  
//需要过滤的属性
   public boolean apply(Object source, String name, Object value) {   //source :源对象   name:属性名 value 属性值 
    if(source instanceof Employee&&(name.equals("devTasks")||name.equals("probClients")||name.equals("role")))
     return true;
    if(source instanceof Role&&(name.equals("actions")||name.equals("emps")))
     return true;
    if(source instanceof DevTask&&(name.equals("devTaskPlans")))
     return true;
    if(source instanceof ProbClient&&(name.equals("devTasks")))
     return true;
    return false;
   }
   
  });
  Map<String,Object> m=new HashMap<String,Object>();
  m.put("index", index);
  m.put("size",size );
  m.put("total",total );
  m.put("pages",pages );
  m.put("devList",devList );
  //m.put("probClient",probClient );
  JSONObject json=JSONObject.fromObject(m,config);

  

  

 

  

 

posted on 2017-08-05 20:52  0X000  阅读(129)  评论(0)    收藏  举报

导航