When using Jsonobject.fromobject, the "there is a cycle in the hierarchy" exception appears.
It means that there is a cycle of death, that is, the model has a cyclic inclusion relationship;
Workaround:
Use Setcycledetectionstrategy to prevent self-contained
Code:
New== json.tostring ();
Dependent Packages:
Import Net.sf.json.JSONArray; Import Net.sf.json.JSONObject; Import Net.sf.json.JsonConfig; import net.sf.json.util.CycleDetectionStrategy;
Full Real column
/*** Here to test if it contains self-contained time need Cycledetectionstrategy*/ Public Static voidTestcycleobject () {Cycleobject object=NewCycleobject (); Object.setmemberid ("Yajuntest"); Object.setsex ("Male"); Jsonconfig Jsonconfig=NewJsonconfig (); Jsonconfig.setcycledetectionstrategy (cycledetectionstrategy.lenient); Jsonobject JSON=Jsonobject.fromobject (object, jsonconfig); SYSTEM.OUT.PRINTLN (JSON); } Public Static voidMain (string[] args) {jsontest.testcycleobject (); }
where Cycleobject.java
Public class cycleobject { private String MemberID; Private String sex; Private This ; // getters && Setters
Json:there is a cycle in the hierarchy!