在使用JSONObject.fromObject的時候,出現“There is a cycle in the hierarchy”異常。
意思是出現了死循環,也就是Model之間有循環包含關係;
解決辦法:
使用setCycleDetectionStrategy防止自包含
代碼:
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
JSONObject json =JSONObject.fromObject(model, jsonConfig);
result = json.toString();
依賴的包:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
參考網頁:
http://www.cnblogs.com/az19870227/archive/2011/09/19/2180993.html