<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 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
    jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy!

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


    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
    >


    仔細(xì)查了一下發(fā)現(xiàn)是hibernate主外鍵關(guān)聯(lián)的錯(cuò),后來就想下json源代碼下來看,發(fā)現(xiàn)大費(fèi)周章都沒搞到j(luò)son源碼,還是老辦法反編譯瞅瞅,發(fā)現(xiàn)JSONArray根據(jù)判斷取得的不同類型調(diào)用相應(yīng)的方法,

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

    而我從hibernate那得到的是list,所以去調(diào)用了_fromCollection方法,而里面的方法發(fā)現(xiàn)一個(gè)問題:該方法會(huì)不斷的拆開實(shí)體屬性,直到?jīng)]有為止,而我的ContactGroup里有兩個(gè)屬性用于自身關(guān)聯(lián)

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


    也就是說主外鍵自身關(guān)聯(lián)的是個(gè)死循環(huán),那怎么才能不讓他出現(xiàn)這種情況呢,應(yīng)該有個(gè)配置的參數(shù)后者終止循環(huán)的地方吧,查看發(fā)
    現(xiàn),jsonConfig,呵呵,config應(yīng)該是配置參數(shù)吧,參看JsonConfig看見巨多的屬性,有點(diǎn)暈PropertyFilter
    ,不提了,看了老半天,發(fā)現(xiàn)了一個(gè)屬性PropertyFilter,PropertyFilter 是一個(gè)interface,代碼如下:


    public interface PropertyFilter
    {


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


    也就是說我可以通過這個(gè)方法過濾掉List里的相應(yīng)屬性,只要讓它返回true就可過濾掉,……,有點(diǎn)懸……我們重寫一下這個(gè)方法:


    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產(chǎn)生的實(shí)體bean中的contactGroups和contactGroupPersons過濾掉就OK了!

    然后調(diào)用JSONArray.fromObject(mychildren,cfg); mychildren是hibernate返回的list。

     

     1List<ShoppingCart> listCarts = sCartServiceImpl
     2                        .ShoppingCartTable(shoppingCart);
     3                // 先過濾對(duì)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                // 將數(shù)據(jù)轉(zhuǎn)換成Json數(shù)據(jù)
    16                JSONArray jsonObject = JSONArray.fromObject(listCarts, config);
    17                System.out.println(jsonObject.toString());
    18

    搞了一下午,參考網(wǎng)絡(luò)的資料!解決問題了!


    評(píng)論

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy   回復(fù)  更多評(píng)論   

    2012-06-26 15:22 by cfm
    請(qǐng)問這個(gè)PropertyFilter接口怎么調(diào)用啊 我在類上實(shí)現(xiàn)這個(gè)接口編譯不通過

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy   回復(fù)  更多評(píng)論   

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

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy [未登錄]  回復(fù)  更多評(píng)論   

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

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy   回復(fù)  更多評(píng)論   

    2013-09-16 09:25 by taojie
    請(qǐng)問要在哪里重寫呢?為什么這里老是報(bào)這個(gè)異常

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy   回復(fù)  更多評(píng)論   

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

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy   回復(fù)  更多評(píng)論   

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

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy   回復(fù)  更多評(píng)論   

    2013-11-26 14:31 by vf
    vddv

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy   回復(fù)  更多評(píng)論   

    2014-04-17 15:57 by hong0220
    已經(jīng)解決了,好用啊

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy   回復(fù)  更多評(píng)論   

    2014-05-02 11:01 by @jack
    不錯(cuò)是那個(gè)問題,出現(xiàn)了死循環(huán),只要把實(shí)體中相應(yīng)的屬性過濾掉就OK,頂

    # re: jQuery調(diào)用JSON時(shí),net.sf.json.JSONException: There is a cycle in the hierarchy [未登錄]  回復(fù)  更多評(píng)論   

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

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 日韩人妻无码免费视频一区二区三区 | 亚洲伊人久久大香线蕉啊| 美女黄频a美女大全免费皮| 一本无码人妻在中文字幕免费| 亚洲宅男天堂在线观看无病毒| 国产免费A∨在线播放| 亚洲中文字幕无码久久精品1 | 99视频免费播放| 老汉色老汉首页a亚洲| 67194成手机免费观看| 免费在线不卡视频| 色多多免费视频观看区一区| mm1313亚洲精品国产| fc2成年免费共享视频网站| 亚洲精品无码AV人在线播放| 亚洲精品免费在线观看| 亚洲精品自在线拍| 日韩免费三级电影| 中文字幕成人免费高清在线 | 亚洲国产视频久久| www.亚洲精品.com| 久久中文字幕免费视频| 亚洲欧洲AV无码专区| 亚洲情a成黄在线观看| 久久久久久免费一区二区三区 | 中文字幕免费在线看电影大全| 久久91亚洲人成电影网站| 蜜臀98精品国产免费观看| 亚洲日韩乱码中文字幕| 精品亚洲一区二区三区在线观看 | 中文在线日本免费永久18近| 亚洲卡一卡2卡三卡4卡无卡三| 永久免费的网站在线观看| 一级中文字幕乱码免费| 久久亚洲精品无码VA大香大香| 全免费一级毛片在线播放| 国产高清对白在线观看免费91| 亚洲欧洲日产国码二区首页| 可以免费观看的一级毛片| 57pao国产成永久免费视频| 无人视频免费观看免费视频|