Atitit. Resolving net.sf.json.JSONException There is a cycle in the hierarchy

Source: Internet
Author: User
Tags set set

Atitit. Resolving net.sf.json.JSONException There is a cycle in the hierarchy

1. Environment : use hibernate4 to follow a ,, or which object serialized into a json Time to have this problem ... 1

2. Cause : HB default lazy mode is caused when The associated object ... 1

3. #--- fix :: Lazy =false ( recommended ).. 1

4. The following four methods can be used to solve the hibernate serialization Problem 2

5. Beanutils.copyproperties can solve ... 2

6. Attribute Filter PropertyFilter 2

7. Simple attribute Filter setexclusions Method 3

8. Jsonbeanprocessor Law 4

9. Set json-lib 's setcycledetectionstrategy property to handle the loop itself (ATI test does not take effect O81) .. 4

10. Reference 5

1. Environment:Usehibernate4with a,,the object of the other is serialized into aJSONit's a problem....

2. Causes:: HBthe defaultLazywhen it comes to the associated object....

3. #---Solve:: Lazy =false (Recommended)..

<!--o7o ati ---

<!--Many starts with a foreign key that represents the table- -

<!--class can not be written, because the value computer (property) based on the name will automatically find the class of which it belongs by reflection --

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < many-to-one  name = "prgrm"   insert = "false"  update = "false"   lazy = "false" >

< column name = "progarmme_id"/>

</many-to-one>

Author:: Old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

4. The following four methods can be used to solveHibernatethe serialization Problem

1 domain class implements jsonstring interface

2 Creating an Jsonconfig instance and configuring the attribute exclusion list

3 Using attribute Filters

4 Write a custom jsonbeanprocessor

5. Beanutils.copyproperties can solve...

gvprogramme stp = (gvprogramme) Arg1;

Gvprogramme o= new gvprogramme();

beanutils. copyproperties (o, stp) ;

6. Attribute FilterPropertyFilter

  //Filter The disassembly of set set first4jsonconfig config =Newjsonconfig ();5Config.setjsonpropertyfilter (NewPropertyFilter () {6@Override7                     Public Booleanapply (Object arg0, String arg1, Object arg2) {8                        if(Arg1.equals ("Shoppingcarts")) {9                            return true;Ten                        } Else { One                            return false; A                        } -                    } -                }); the                //Convert data to JSON data -Jsonarray jsonobject = jsonarray.fromobject (listcarts, config); -System.out.println (jsonobject.tostring ());

7. Simple attribute FiltersetexclusionsMethod

2. The second method makes it easy to add and remove attributes that contain and need to be excluded through Jsonconfig instances

[Java] View Plain Copy Print ?

5 public class Person {<br>

6 Private String Name;<br>

7 Private String Lastname;<br>

8 Private Address Address;<br>

9 <br>

//Getters & Setters<br>

}<br>

<br>

Jsonconfig jsonconfig = new Jsonconfig ();<br>

Jsonconfig.setexclusions (New string[]{"Address"});<br>

The person bean =/* Initialize */;<br>

JSON json = Jsonserializer.tojson (bean, jsonconfig);

Note: This method does not differentiate between target classes, which means that if there are "address" attributes in all 2 beans, the address attribute in the two beans will be excluded

3. Using PropertyFilter allows you to control both properties and classes that need to be excluded, which can also be bidirectional or applied to JSON strings to Java objects

Public Actionforward Execute (actionmapping mapping, actionform form, httpservletrequest request, HttpServletResponse Response) {

Libtypedao Libtypedao = new Libtypedao ();

list<libtype> list = Libtypedao.findall ();

Jsonconfig jsonconfig = new Jsonconfig (); setting up a configuration file

Jsonconfig.setignoredefaultexcludes (FALSE); Set Default Ignore

Jsonconfig.setexcludes (New string[]{"Libs"}); here is the highlight, as long as the desired ignore field is added to the array, in the above case, to ignore the "Libs", then add it to the array, in the actual test, I found in the returned array, there are a lot of useless properties, such as " multipartrequesthandler","servletwrapper", you can also add these two to the ignored array .

8. Jsonbeanprocessor method

4. Finally look at Jsonbeanprocessor, which is similar to the implementation of jsonstring, returning a legitimate jsonobject representing the original domain class

1. set The json-lib to filter out the fields that cause the loop :

9. SettingsJson-libof theSetcycledetectionstrategyproperty to handle loops on its own(ATItest does not take effectO81). .

2.

3. Easy But the data is too complex to cause data overflow or inefficient.

Public Actionforward Execute (actionmapping mapping, actionform form, httpservletrequest request, HttpServletResponse Response) {

Libtypedao Libtypedao = new Libtypedao ();

list<libtype> list = Libtypedao.findall ();

Jsonconfig jsonconfig = new Jsonconfig (); setting up a configuration file

Jsonconfig.setignoredefaultexcludes (FALSE); Set Default Ignore

Jsonconfig.setcycledetectionstrategy (cycledetectionstrategy.lenient); here is the bright spot, but after testing, the first 2 methods Some tragedies, although can be used, but the results seem to circulate several times, as for why, also please expert guidance.

Jsonarray Jsonarray = Jsonarray.fromobject (list,jsonconfig); Load configuration file

return null;

}

3.

10. Reference

About Json-lib There is a cycle in the hierarchy! Problem 3 Solutions _ William Manor _williambryantliu's and news blog. htm

Serialization and deserialization of JSON with Json-lib-farewell to late autumn-blog channel-CSDN.NET.htm

Lazy usage in Hibernate-toyoe-blog Park. htm

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.