There is a cycle in the hierarchy! 问题解决

前端之家收集整理的这篇文章主要介绍了There is a cycle in the hierarchy! 问题解决前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在做Hibernate 映射对象转换json 的时候,常常会遇到如下异常:

  1. net.sf.json.JSONException: There is a cycle in the hierarchy!
  2. at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
  3. at net.sf.json.JSONObject._fromBean(JSONObject.java:859)
  4. at net.sf.json.JSONObject.fromObject(JSONObject.java:194)
  5. at net.sf.json.JSONObject._processValue(JSONObject.java:2793)
  6. at net.sf.json.JSONObject._setInternal(JSONObject.java:2817)
  7. at net.sf.json.JSONObject.setValue(JSONObject.java:1527)
  8. at net.sf.json.JSONObject._fromBean(JSONObject.java:946)
  9. at net.sf.json.JSONObject.fromObject(JSONObject.java:194)

解决方法

  1. JsonConfig jsonConfig = new JsonConfig();
  2. jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); // 自动为我排除circle。
  3.  
  4. jsonConfig.setExcludes(new String[] { "user" }); // 设置转换中忽略的属性
  5.  
  6. JSONArray.fromObject(sourceList,jsonConfig)

猜你在找的Json相关文章