atitit.解決net.sf.json.JSONException There is a cycle in the hierarchy

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   使用   os   

 

atitit.解決net.sf.json.JSONException There is a cycle in the hierarchy

1. 環境:使用hibernate4跟個,,要不個哪的對象系列化成個json的時候兒有這個問題了... 1

2. 原因::hb預設的lazy 方式造成的當有關聯對象的時候兒... 1

3. #---解決::lazy =false  (推薦).. 1

4. 別的有以下的四個方法可以解決hibernate的序列化問題 2

5. BeanUtils.copyProperties  可以解決... 2

6. 屬性過濾器PropertyFilter 2

7. 簡單屬性過濾器setExclusions法 3

8. JsonBeanProcessor法 4

9. 設定JSON-LIB的setCycleDetectionStrategy屬性讓其自己處理迴圈(ati測試不生效O81).. 4

10. 參考 5

 

1. 環境:使用hibernate4跟個,,要不個哪的對象系列化成個json的時候兒有這個問題了...

 

2. 原因::hb預設的lazy 方式造成的當有關聯對象的時候兒...

 

3. #---解決::lazy =false  (推薦)..

  <!-- o7o ati -->

         <!-- many開頭的是代表該表持有外鍵 -->  

           <!-- class可以不寫,因為根據name的值computer(屬性),會通過反射自動找到屬於哪個類的 -->    

        <many-to-one name="prgrm"  insert="false" update="false"  lazy="false">

          <column name="progarmme_id" />   

        </many-to-one>

        作者:: 老哇的爪子 Attilax 艾龍,  EMAIL:[email protected]

轉載請註明來源: http://blog.csdn.net/attilax

 

4. 別的有以下的四個方法可以解決hibernate的序列化問題

1 domain類實現JSONString介面

2 建立JsonConfig執行個體,並配置屬性排除清單

3 用屬性過濾器

4 寫一個自訂的JsonBeanProcessor

 

 

5. BeanUtils.copyProperties  可以解決...

 GvProgramme stp=(GvProgramme) arg1;

GvProgramme o=new GvProgramme();

 BeanUtils.copyProperties(o, stp);

 

 

6. 屬性過濾器PropertyFilter

 

 

  // 先過濾對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());

 

 

7. 簡單屬性過濾器setExclusions法

2.第二種方法通過jsonconfig執行個體,對包含和需要排除的屬性進行方便添加刪除

[java]view plaincopyprint?

 

5 public class Person {<br>  

6    private String name;<br>  

7    private String lastname;<br>  

8    private Address address;<br>  

9  <br>  

10    // getters & setters<br>  

11 }<br>  

12  <br>  

13 JsonConfig jsonConfig = new JsonConfig();<br>  

14 jsonConfig.setExclusions( new String[]{ "address" } );<br>  

15 Person bean = /* initialize */;<br>  

16 JSON json = JSONSerializer.toJSON( bean, jsonConfig );  

 

 

注意:這種方法不區分目標類,就是說如果有2個bean當中都存在“address”屬性,那麼採用這種方法,這兩個bean中的address屬 性都將被排除

3. 使用propertyFilter可以允許同時對需要排除的屬性和類進行控制,這種控制還可以是雙向的,也可以應用到json字串到java對象

 

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

  LibtypeDAO libtypeDAO = new LibtypeDAO();

  List<Libtype> list = libtypeDAO.findAll();

  JsonConfig jsonConfig = new JsonConfig();  //建立設定檔

  jsonConfig.setIgnoreDefaultExcludes(false);  //設定預設忽略

  jsonConfig.setExcludes(new String[]{"libs"});  //此處是亮點,只要將所需忽略欄位加到數組中即可,在上述案例中,所要忽略的是“libs”,那麼將其添到數組中即可,在實際測試中,我發現在所返回 數組中,存在大量無用屬性,如“multipartRequestHandler”,“servletWrapper”,那麼也可以將這兩個加到忽略數組 中.

 

8. JsonBeanProcessor法

4. 最後來看JsonBeanProcessor,這種方式和實現JsonString很類似,返回一個代表原來的domain類的合法JSONOBJECT

1.設定JSON-LIB讓其過濾掉引起迴圈的欄位:

 

 

9. 設定JSON-LIB的setCycleDetectionStrategy屬性讓其自己處理迴圈(ati測試不生效O81)..

2.

3.省事但是資料過於複雜的話會引起資料溢出或者效率低下。

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

LibtypeDAO libtypeDAO = new LibtypeDAO();

List<Libtype> list = libtypeDAO.findAll();

JsonConfig jsonConfig = new JsonConfig(); //建立設定檔

jsonConfig.setIgnoreDefaultExcludes(false); //設定預設忽略

jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);   //此處是亮點,不過經過測試,第2種方法有些悲劇,雖然可以使用,但其結果貌似迴圈數次,至於為啥,還請高人指點。

JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig); //載入設定檔

return null;

}

3.

 

10. 參考

關於json-lib There is a cycle in the hierarchy!問題的3種解決辦法_威廉莊園_williambryantliu的和訊部落格.htm

使用json-lib完成json的序列化和還原序列化 - 話別深秋 - 部落格頻道 - CSDN.NET.htm

hibernate中lazy的使用 - TOYOE - 部落格園.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.