---
title: "net.sf.json.JSONException: There is a cycle in the hierarchy!"
author: "fish_bone"
date: "2011-11-10T14:32:46Z"
tags: []
categories: ["Web前端"]
slug: "net-sf-json-jsonexception-there-is-a-cycle-in-the-hierarchy"
draft: false
lastmod: "2011-11-10T14:32:46Z"
id: 1250257
url: "https://fish-bone.iteye.com/blog/1250257"
---

java程序返回json数据给extjs 前端时，可能会报net.sf.json.JSONException: There is a cycle in the hierarchy! ，此时是因为设置了hibernate关联关系，  
&lt; set  name ="areas"  inverse ="true" &gt;  
          &lt; key &gt;  
           &lt; column  name ="mid"  not-null ="true"   /&gt;  
          &lt;/ key &gt;  
          &lt; one-to-many  class ="Area" /&gt;  
         &lt;/ set &gt;

设置自身关联的组对象

public   class  Map {  
     private  Integer mid;  
     private  Integer x;  
     private  Integer y;  
     private  String url;  
     private  Set &lt; Area &gt;  areas  =   new  HashSet &lt; Area &gt; ();

通过JSON-LIB来过滤关联的集合属性，代码如下：

 

JsonConfig config  =   new  JsonConfig();

//用过滤  
config.setJsonPropertyFilter( new  PropertyFilter(){  
     public   boolean  apply(Object source, String name, Object value) {  
         if (name.equals( " areas " )) { //要过滤的areas ，Map对象中的  
             return   true ;  
        }  else  {  
             return   false ;  
        }  
    }  
});  

                
JSONObject json  =  JSONObject.fromObject( ma , config);  // / ma 为要转成json的 Map 对象