json-lib出現(xiàn)There is a cycle in the hierarchy解決辦法
更新時間:2010年02月24日 19:17:41 作者:
如果需要解析的數(shù)據(jù)間存在級聯(lián)關(guān)系,而互相嵌套引用,在hibernate中極容易嵌套而拋出net.sf.json.JSONException: There is a cycle in the hierarchy異常。
解決辦法
1.設(shè)置JSON-LIB讓其過濾掉引起循環(huán)的字段。
Java代碼
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register
config.setExcludes(new String[]{//只要設(shè)置這個數(shù)組,指定過濾哪些字段。
"consignee",
"contract",
"coalInfo",
"coalType",
"startStation",
"balanceMan",
"endStation"
});
String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}";
out.print(tempStr);
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register
config.setExcludes(new String[]{//只要設(shè)置這個數(shù)組,指定過濾哪些字段。 "consignee", "contract", "coalInfo", "coalType", "startStation", "balanceMan", "endStation" }); String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}"; out.print(tempStr);
2.設(shè)置JSON-LIB的setCycleDetectionStrategy屬性讓其自己處理循環(huán),省事但是數(shù)據(jù)過于復(fù)雜的話會引起數(shù)據(jù)溢出或者效率低下。
Java代碼
[code]
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register
String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}";
out.print(tempStr);
1.設(shè)置JSON-LIB讓其過濾掉引起循環(huán)的字段。
Java代碼
復(fù)制代碼 代碼如下:
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register
config.setExcludes(new String[]{//只要設(shè)置這個數(shù)組,指定過濾哪些字段。
"consignee",
"contract",
"coalInfo",
"coalType",
"startStation",
"balanceMan",
"endStation"
});
String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}";
out.print(tempStr);
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register
config.setExcludes(new String[]{//只要設(shè)置這個數(shù)組,指定過濾哪些字段。 "consignee", "contract", "coalInfo", "coalType", "startStation", "balanceMan", "endStation" }); String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}"; out.print(tempStr);
2.設(shè)置JSON-LIB的setCycleDetectionStrategy屬性讓其自己處理循環(huán),省事但是數(shù)據(jù)過于復(fù)雜的話會引起數(shù)據(jù)溢出或者效率低下。
Java代碼
[code]
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register
String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}";
out.print(tempStr);
相關(guān)文章
JSON 入門教程基礎(chǔ)篇 json入門學(xué)習(xí)筆記
剛開始接觸json的時候感覺有點(diǎn)奇怪的命名,后來使用才發(fā)現(xiàn)這么簡單而且用好用,擴(kuò)展性很強(qiáng),這里就為大家整理一下2020-09-09如何實(shí)現(xiàn)json數(shù)據(jù)可視化詳解
最近在工作中開發(fā)一個內(nèi)部功能時碰到的一個需求,要把json數(shù)據(jù)在頁面上展示出來,平時瀏覽器會安裝jsonView這樣的擴(kuò)展來看json數(shù)據(jù),但是程序要用到的話該怎么辦呢?今天在網(wǎng)上搜索的時候,發(fā)現(xiàn)了這個小技巧,分享給大家,有需要的朋友們可以參考借鑒,下面來一起看看吧2016-11-11Ajax+Json 級聯(lián)菜單實(shí)現(xiàn)代碼
Ajax+Json 級聯(lián)菜單實(shí)現(xiàn)代碼,需要的朋友可以參考下。2009-10-10Javascript生成json的函數(shù)代碼(可以用php的json_decode解碼)
這幾天迷上了JSON,連項(xiàng)目中一些XML的工作都交給了JSON,話說JSON真的比XML方便的多啊2012-06-06