json-lib There is a cycle in the hierarchy!

若在hibernate中使用了級聯,當使用JSONArray或JSONObject格式時便會報

json-lib There is a cycle in the hierarchy!

的錯誤,在網上查了基本上有3中方法,分別是(此處引用別人總結的方法)
1.設置JSON-LIB讓其過濾掉引起循環的字段;
2.設置JSON-LIB的setCycleDetectionStrategy屬性讓其自己處理循環,省事但是數據過於複雜的話會引起數據溢出或者效率低下;
3.最爲原始的辦法,自己寫個JavaBean,用forEach循環,添加到List中.
這裏本人是採用了JsonFilter過濾關鍵字,這裏寫了一個類
public class JsonFilter {
	public static JsonConfig getFilter(final String [] str){
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter(){
			public boolean apply(Object source,String name, Object value){
				if(judge(str, name)){
					return true;
				}else{
					return false;
				}
			}
			
			public boolean judge(String [] s, String str2){
				boolean b = false;
				for (String str1 : str){
					if(str2.equals(str1)){
						b = true;
					}
				}
				return b;
			}
		});
		return config;
	}
}
這樣過濾時直接使用JsonFileter.getFilter(new String[] {""})即可實現過濾,而不出現問題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章