天天看點

json轉換資料時候,報there is a cycle in the hierarchy!

//設定json過濾主外鍵,防止出現死循環there is a cycle in the hierarchy! 
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter(){//過濾屬性
			//其中houses,district就是我們要過濾的字段,也就是JavaBean中的屬性名
			public boolean apply(Object arg0, String name, Object arg2) {
				if(name.equals("houses")||name.equals("district")){
					return true;
				}
				return false;
			}
			
		});
		//在轉換的時候加個config
		JSONArray jsarr = JSONArray.fromObject(streetList,config);
		request.setAttribute("jsarr", jsarr);
		}
           

這個異常,是由于,json在轉換資料的時候,能夠檢測出要轉換的屬性之間存在着主外鍵關系,特别是hibernate在操作資料的時候,造成了死循環,解決辦法,就是用JsonConfig提供的一個過濾屬性的方法,實作裡面的apply的方法,然後将要過濾的屬性進行判斷。這樣就可以解決這個問題了。