<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ply

    吞噬黑暗
    posts - 1, comments - 11, trackbacks - 0, articles - 13
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
    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。

     

     1List<ShoppingCart> listCarts = sCartServiceImpl
     2                        .ShoppingCartTable(shoppingCart);
     3                // 先過濾對set集合的拆解
     4                JsonConfig config = new JsonConfig();
     5                config.setJsonPropertyFilter(new PropertyFilter() {
     6                    @Override
     7                    public boolean apply(Object arg0, String arg1, Object arg2) {
     8                        if (arg1.equals("shoppingCarts")) {
     9                            return true;
    10                        }
     else {
    11                            return false;
    12                        }

    13                    }

    14                }
    );
    15                // 將數據轉換成Json數據
    16                JSONArray jsonObject = JSONArray.fromObject(listCarts, config);
    17                System.out.println(jsonObject.toString());
    18

    搞了一下午,參考網絡的資料!解決問題了!


    評論

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy   回復  更多評論   

    2012-06-26 15:22 by cfm
    請問這個PropertyFilter接口怎么調用啊 我在類上實現這個接口編譯不通過

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy   回復  更多評論   

    2012-12-07 13:49 by upup
    @cfm
    你只要把這個接口當成一個參數放到fromObject里面就行了。
    JSONArray jsonObject = JSONArray.fromObject(listCarts, config);

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy [未登錄]  回復  更多評論   

    2012-12-11 16:30 by Rose
    太感謝了,我正好碰到這個問題了。

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy   回復  更多評論   

    2013-09-16 09:25 by taojie
    請問要在哪里重寫呢?為什么這里老是報這個異常

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy   回復  更多評論   

    2013-09-16 09:41 by taojie
    shoppingCarts指的是哪里的屬性還是?

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy   回復  更多評論   

    2013-10-25 11:51 by er
    根本就行不通

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy   回復  更多評論   

    2013-11-26 14:31 by vf
    vddv

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy   回復  更多評論   

    2014-04-17 15:57 by hong0220
    已經解決了,好用啊

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy   回復  更多評論   

    2014-05-02 11:01 by @jack
    不錯是那個問題,出現了死循環,只要把實體中相應的屬性過濾掉就OK,頂

    # re: jQuery調用JSON時,net.sf.json.JSONException: There is a cycle in the hierarchy [未登錄]  回復  更多評論   

    2016-03-01 17:13 by monkey
    樓主太給力了,謝謝你的分享,好人一生平安!!!

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲人成电影在线播放| 一级女性全黄生活片免费看| 久久精品亚洲福利| 国产精品成人免费一区二区| 91免费在线视频| 无码日韩人妻AV一区免费l| 久久亚洲最大成人网4438| 亚洲AV电影院在线观看| 久久久久亚洲AV无码专区桃色| 蜜臀91精品国产免费观看| 亚洲一区在线免费观看| 精品一区二区三区免费| 久久一区二区三区免费| 黄色网址免费在线| 欧美色欧美亚洲另类二区| 97se亚洲国产综合自在线| 亚洲精品乱码久久久久久下载| 亚洲人精品午夜射精日韩| 亚洲成av人片天堂网老年人| 破了亲妺妺的处免费视频国产| 无码区日韩特区永久免费系列| 免费无码VA一区二区三区| 久草视频在线免费看| 久久免费视频网站| 伊人免费在线观看高清版| 两个人看的www视频免费完整版| 免费人成视频在线播放| 老司机午夜在线视频免费| 国产亚洲美女精品久久| WWW国产亚洲精品久久麻豆| 亚洲人成人伊人成综合网无码| 亚洲人精品亚洲人成在线| 国产成人精品日本亚洲直接| 亚洲一区二区久久| 国产成人精品日本亚洲直接| 亚洲色偷偷偷综合网| 亚洲国产成人久久精品大牛影视| 亚洲国产精品ⅴa在线观看| 色偷偷亚洲男人天堂| 黄色网页免费观看| 91精品成人免费国产|