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

Source: Internet
Author: User

Problem:

Net. sf. json. JSONException: There is a cycle in the hierarchy!
At net. sf. json. util. CycleDetectionStrategy $ StrictCycleDetectionStrategy. handleRepeatedReferenceAsObject (CycleDetectionStrategy. java: 97)

**************************************** **************************************** **************************************** ***************************

After checking some information, I will summarize the two methods to solve the problem. I 'd like to share with you.

1. Solve the Problem Based on the Principle. If the data to be parsed has cascading relationships and mutual nested references, it is very easy to nest and throw the net in hibernate. sf. json. JSONException: There is a cycle in the hierarchy exception.

For example, there are two tables: Experiment (Lib) and Class (Libtype). Each experiment corresponds to a class. In the POJO of the class, the following code is displayed:

Private Integer ltid; // category ID
Private String ltype; // category name
Private Set libs = new HashSet (0); // corresponding lab Set

When we write the following code, an error is reported:

Public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ){

LibtypeDAO libtypeDAO = new LibtypeDAO ();
List <Libtype> list = libtypeDAO. findAll ();
JSONArray jsonArray = JSONArray. fromObject (list );

Return null;

}

The reason is very simple. In Libtype, there is a List-independent attribute value, that is, libs. We only need ltid and ltype, so an error is returned.

Based on the information I searched online, there are three solutions:

1. Set the JSON-LIB to let it filter out fields that cause loops:

Public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ){

LibtypeDAO libtypeDAO = new LibtypeDAO ();
List <Libtype> list = libtypeDAO. findAll ();
JsonConfig jsonConfig = new JsonConfig (); // create a configuration file
JsonConfig. setIgnoreDefaultExcludes (false); // set to ignore by default
JsonConfig. setExcludes (new String [] {"libs"}); // This is a bright spot. You only need to add the ignored fields to the array. In the above case, to ignore the "libs", add it to the array. In actual tests, I found that a large number of useless attributes exist in the returned array, such as "multipartRequestHandler ", "servletWrapper", you can also add the two to the ignore array.
JSONArray jsonArray = JSONArray. fromObject (list, jsonConfig); // load the configuration file
Return null;

}

2. Set the setCycleDetectionStrategy attribute of the JSON-LIB to allow it to process loops on its own, saving trouble but too complicated data can cause data overflow or inefficiency.

Public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ){

LibtypeDAO libtypeDAO = new LibtypeDAO ();
List <Libtype> list = libtypeDAO. findAll ();
JsonConfig jsonConfig = new JsonConfig (); // create a configuration file
JsonConfig. setIgnoreDefaultExcludes (false); // set to ignore by default
JsonConfig. setCycleDetectionStrategy (CycleDetectionStrategy. LENIENT); // This is a bright spot. However, after testing, the 2nd methods are a little tragic. Although they can be used, the results seem to be repeated several times. For the reason, please kindly advise.
JSONArray jsonArray = JSONArray. fromObject (list, jsonConfig); // load the configuration file
Return null;

}

3. the most primitive method is to write a JavaBean by yourself and add it to the List using the forEach loop. In this way, I think someone on the Internet has succeeded. I didn't try it, but I can probably write it out in the process, the correctness of the results is to be verified.

JavaBean:

Public LibtypeForm {

Int ltid;

String ltname;

}

Public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ){

LibtypeDAO libtypeDAO = new LibtypeDAO ();
List <Libtype> list = libtypeDAO. findAll ();

List <LibtypeForm> formList = new ArrayList ();
For (Libtype libtype: list ){

LibtypeForm form = new LibtypeForm ();

Form. setLtid (libtype. getLtid );

Form. setLtname (libtype. getLtname );

FormList. add (form );

}
JSONArray jsonArray = JSONArray. fromObject (formList );
Return null;

}

If you have a better method, please kindly advise.

 

Related Article

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.