jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy

2012年3月16日

jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy!

遇到了一些問題,如hibernate延遲加載錯誤,這都是老掉牙的問題了,一看就知道加個lazy=flase就OK了。想不到快要完成了又遇到了新的問題,JSON死循環,實在讓人鬱悶。異常如下:


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:674)
        at net.sf.json.JSONObject.fromObject(JSONObject.java:181)
        at net.sf.json.JSONArray._processValue(JSONArray.java:2381)
        at net.sf.json.JSONArray.processValue(JSONArray.java:2412)
        Truncated. see log file for complete stacktrace
>


仔細查了一下發現是hibernate主外鍵關聯的錯,後來就想下json源代碼下來看,發現大費周章都沒搞到json源碼,還是老辦法反編譯瞅瞅,發現JSONArray根據判斷取得的不同類型調用相應的方法,

if (object instanceof Collection)
    return _fromCollection((Collection)object, jsonConfig);

而我從hibernate那得到的是list,所以去調用了_fromCollection方法,而裏面的方法發現一個問題:該方法會不斷的拆開實體屬性,直到沒有爲止,而我的ContactGroup裏有兩個屬性用於自身關聯

private Set contactGroups = new HashSet(0);
private Set contactGroupPersons = new HashSet(0);


也就是說主外鍵自身關聯的是個死循環,那怎麼才能不讓他出現這種情況呢,應該有個配置的參數後者終止循環的地方吧,查看發
現,jsonConfig,呵呵,config應該是配置參數吧,參看JsonConfig看見巨多的屬性,有點暈PropertyFilter
,不提了,看了老半天,發現了一個屬性PropertyFilter,PropertyFilter 是一個interface,代碼如下:


public interface PropertyFilter
{


public abstract boolean apply(Object obj, String s, Object obj1);
}


也就是說我可以通過這個方法過濾掉List裏的相應屬性,只要讓它返回true就可過濾掉,……,有點懸……我們重寫一下這個方法:


JsonConfig cfg = new JsonConfig();
    cfg.setJsonPropertyFilter(new PropertyFilter()
    {
         public boolean apply(Object source, String name, Object value) {
           if(name.equals("contactGroups")||name.equals("contactGroupPersons")) {
             return true;
           } else {
             return false;
          }
        }
       });

將hibernate產生的實體bean中的contactGroups和contactGroupPersons過濾掉就OK了!

然後調用JSONArray.fromObject(mychildren,cfg); mychildren是hibernate返回的list。

 

發佈了33 篇原創文章 · 獲贊 25 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章