欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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代碼
復(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)文章

最新評論