当前位置: 代码迷 >> JavaScript >> net.sf.json.JSONException: There is a cycle in the hierarchy
  详细解决方案

net.sf.json.JSONException: There is a cycle in the hierarchy

热度:1079   发布时间:2012-08-21 13:00:22.0
net.sf.json.JSONException: There is a cycle in the hierarchy!
11-4-26 11:18:46 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
net.sf.json.JSONException: There is a cycle in the hierarchy!
	at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
	at net.sf.json.JSONObject._fromBean(JSONObject.java:657)
	at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
	at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
	at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
	at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
	at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
	at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
	at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
	at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
	at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
	at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
	at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
	at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
	at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
	at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
	at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
	at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240)
	at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
	at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
	at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
	at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
	at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
	at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
	at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
	at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
	at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
	at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
	at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
	at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
	at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
	at net.sf.json.JSONArray.fromObject(JSONArray.java:105)
	at com.hbsi.struts.action.UserAction.UserList(UserAction.java:56)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
	at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
	at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:619)

?

?

?错误原因:json中的

?

?

	UserDaoImpl udi = new UserDaoImpl();
		List<User> list = new ArrayList<User>();
		list = udi.listUser();
		List<UserUtil> userUtil = new ArrayList<UserUtil>();		
		JSONArray jsor = JSONArray.fromObject(list);
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(jsor);
		return null;

?项目中使用了AJAX技术,JAR包为:json-lib.jar, 在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题:

?主要是json中支持关联

主外键关联,产生循环错误

需设置过滤,去掉关联

?

?

?

解决如下

?

?

UserDaoImpl udi = new UserDaoImpl();
		List<User> list = new ArrayList<User>();
		list = udi.listUser();
		//兼容hibernate和json
		List<UserUtil> userUtil = new ArrayList<UserUtil>();
		for (User u : list) {
			UserUtil uu = new UserUtil();
			uu.setUsername(u.getUsername());
			uu.setId(u.getId());
			userUtil.add(uu);
		}		
		JSONArray jsor = JSONArray.fromObject(userUtil);
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(jsor);
		return null;

?

?

?

  相关解决方案