现在的位置: 首页 > 综合 > 正文

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

2011年08月26日 ⁄ 综合 ⁄ 共 892字 ⁄ 字号 评论关闭

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

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

 

因为Hibernate中设置了自身关联:

  <set name="areas" inverse="true">
         
<key>
          
<column name="mid" not-null="true" />
         
</key>
         
<one-to-many class="Area"/>
        
</set>

 

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

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

 

通过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 对象

 

 

抱歉!评论已关闭.