Json:there is a cycle in the hierarchy!

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.