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

Source: Internet
Author: User

Turn from: 24351051

Cause Analysis:

This exception is due to the fact that the Jsonobject plug-in will infinitely disassemble the object you passed into, until there is no detachable solution, when parsing the bean, there is a dead loop call, that is, there is a mutual invocation between multiple beans. The problem is, if you pass in the object has a foreign key relationship, or reference, then the internal will die loop, will throw this exception solution, we first say a kind of online general: Filter Good, filtering will definitely solve the problem, filtering also has two methods:

Workaround One: Filter out the properties that cause the dead loop call in the Bean:

list<project> projectlist = Projectservices.find ();  Get Data//custom Jsonconfig used to filter the recursive data generated by Hibernate configuration file Jsonconfig config = new Jsonconfig (); Config.setexcludes (New string[]{ "DocumentSet", "Milestoneset", "Issuesset", "Userset"});  As long as you set this array, you specify which fields to filter.   //Composition JSON array Jsonarray json = jsonarray.fromobject (projectlist, config);

This method takes an array, which is the field you need to filter, and it's easy to do.

Solution II

Jsonconfig.setjsonpropertyfilter (New PropertyFilter () {              @Override public              Boolean apply (Object source, String Name, Object value) {                  if (name.equals ("Qualitychecks")) {                      return true;                  }                  return false;              }          

  

Next is what I want to say, in fact, these two methods, there is a kind of disadvantage

If we have a property in a user object that is a department, it refers to the object of organzition, and if we don't do any processing, Calling the Jsonobject.fromobject method will also throw an exception, if we filter the Organzition attribute filtering, then in the foreground display, will not see the relevant departments of any information, in fact, need to show not much, such as only a department name can, but filter out nothing, this Time, many students will build a VO class to encapsulate the properties required by the foreground, which undoubtedly increases the workload and the Code redundancy, LZ is the question has been troubled for a long time, so give a solution.

Borrowing the Jsonvalueprocessor interface in Jsonobject, we implement the interface ourselves, the code is as follows:

/** * Solution Jsonobject.fromobject throws "There is a cycle in the hierarchy" exception resulting in a dead loop solution * @author Luoyu * @date 2012-11-23 */  public class Objectjsonvalueprocessor implements Jsonvalueprocessor {/** * need to leave an array of fields */private            String[] Properties;            /** * Complex attribute types that need to be processed */private class<?> clazz; /** * Construction method, parameters must * @param properties * @param clazz */Public Objectjsonvalueprocessor (string[] pr          Operties,class<?> clazz) {this.properties = properties;      This.clazz =clazz;      } @Override public Object Processarrayvalue (object value, Jsonconfig jsonconfig) {return ""; } @Override Public Object processobjectvalue (String key, object value, Jsonconfig jsonconfig) {Prope          Rtydescriptor PD = NULL;          method = null;          StringBuffer json = new StringBuffer ("{");  try{for (int i=0;i<properties.length;i++) {                PD = new PropertyDescriptor (Properties[i], clazz);                  method = Pd.getreadmethod ();                  String v = string.valueof (Method.invoke (value));                  Json.append ("'" +properties[i]+ "': '" +v+ "'");              Json.append (i! = properties.length-1? ",": ");          } json.append ("}");          }catch (Exception e) {e.printstacktrace ();      } return Jsonobject.fromobject (Json.tostring ());  }          }

In Processobjectvalue This method rewrite, see the code, in addition, in the construction method I gave two parameters, one is to leave the property name, passed through the array, the other is a class<?> type, It is also related to the example above that the organzition.class is used to invoke the Java reflection mechanism later through the class to get the value of the property, after the corresponding value is taken to assemble into a string, Finally through the jsonobject.fromobject to output, not so the output will have a problem, as to what the problem, curious students to try their own

Here's how to call it:

  

Jsonconfig.registerjsonvalueprocessor (Organzition.class, New Objectjsonvalueprocessor (New String[]{"OrgName", " OrgId "},organzition.class)");  

Where Organzition.class is the type of attribute you want to handle

  

Net.sf.json.JSONException: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.