JSON parsing occurs when Net.sf.json.JSONException:There is a cycle in the hierarchy!

Source: Internet
Author: User
Tags string tags

Cause analysis when parsing beans, there is a dead loop call, that is, there are calls between multiple beans. Workaround: Associate the entity objects in the association relationship

set the Lazy property to false to filter out the properties of the bean that caused the dead loop call. (Two kinds of filtration methods)

Filter the associated entity objects in an array way

jsonconfig jsonconfig = new Jsonconfig ();

Jsonconfig.setignoredefaultexcludes (FALSE);


Jsonconfig.setcycledetectionstrategy (cycledetectionstrategy.lenient);
Jsonconfig.setexcludes (new string[] {"Articlecomment", "article",

"Commentator"});


Overriding the filter association object for the Apply method

Jsonconfig.setjsonpropertyfilter (New PropertyFilter () {

@Override
public Boolean apply (object source, String name, Object object) {
if (Name.equals ("article") | | | name.equals ("articlecomment")
|| Name.equals ("commentator")) {
return true;
}
return false;
}

});

The above two methods can solve the problem but I'm not able to get the properties of the associated entity object because the associated entity objects are filtered.

Custom Bean (if you want to associate an object's properties)

Atriclecommentuitl.java

Package com.smsv.app.util;


Import Java.util.Date;


public class Articlecommentutil {
Private Long ID;
Private Date Commenttime;
Private String content;
Private String commentator;
Private String Replyer;


Public Long getId () {
return ID;
}


public void SetId (Long id) {
This.id = ID;
}


Public Date Getcommenttime () {
return commenttime;
}


public void Setcommenttime (Date commenttime) {
This.commenttime = Commenttime;
}


Public String getcontent () {
return content;
}


public void SetContent (String content) {
this.content = content;
}


Public String Getcommentator () {
return commentator;
}


public void Setcommentator (String commentator) {
This.commentator = commentator;
}


Public String Getreplyer () {
return replyer;
}


public void Setreplyer (String replyer) {
This.replyer = Replyer;
}


}



Article.java

Package Com.smsv.app.model;


Import java.io.Serializable;
Import Java.util.Date;
Import Java.util.HashSet;
Import Java.util.Set;


Import Javax.persistence.CascadeType;
Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.FetchType;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
Import Javax.persistence.JoinColumn;
Import Javax.persistence.ManyToMany;
Import Javax.persistence.ManyToOne;
Import Javax.persistence.OneToMany;
Import javax.persistence.Table;


/**
* Copyright? All rights Reserved.
*/
@Entity
@Table (name = "articles")
Public class article implements Serializable {
Private Long ID;
Private String pathcategory;
Private String contentcategory;
Private String title;
Private String body;
private String tags;


Private Date CreationTime;
Private User creator;
Private Careerpath Careerpath;


Private set<collectionarticle> collectionarticles = new hashset<collectionarticle> ();
Private set<articlecomment> articlecomments = new hashset<articlecomment> ();
Private Boolean deleted;


Public article () {
TODO auto-generated Constructor stub
}


Public article (string pathcategory, string tags) {
This.pathcategory = pathcategory;
This.tags = tags;
}


@Id
@GeneratedValue (strategy = Generationtype.auto)
Public Long getId () {
return ID;
}


public void SetId (Long id) {
This.id = ID;
}


Public String getpathcategory () {
return pathcategory;
}


public void Setpathcategory (String pathcategory) {
This.pathcategory = pathcategory;
}


Public String getcontentcategory () {
return contentcategory;
}


public void Setcontentcategory (String contentcategory) {
This.contentcategory = contentcategory;
}


Public String GetTitle () {
return title;
}


public void Settitle (String title) {
This.title = title;
}


@Column (length = 1000)
Public String GetBody () {
return body;
}


public void Setbody (String body) {
This.body = body;
}


Public String GetTags () {
return tags;
}


public void Settags (String tags) {
This.tags = tags;
}


Public Date GetCreationTime () {
return creationtime;
}


public void Setcreationtime (Date creationtime) {
This.creationtime = CreationTime;
}


@ManyToOne (fetch = Fetchtype.eager)
@JoinColumn (name = "creator_id")
Public User Getcreator () {
return creator;
}


public void Setcreator (User creator) {
This.creator = Creator;
}


@OneToMany (fetch = Fetchtype.eager, Mappedby = "ARTICLE", cascade = {Cascadetype.remove})
Public set<collectionarticle> Getcollectionarticles () {
return collectionarticles;
}


public void Setcollectionarticles (set<collectionarticle> collectionarticles) {
This.collectionarticles = Collectionarticles;
}


@OneToMany (fetch = fetchtype.eager, cascade = {Cascadetype.remove}, Mappedby = "article")
Public set<articlecomment> getarticlecomments () {
return articlecomments;
}


public void Setarticlecomments (set<articlecomment> articlecomments) {
This.articlecomments = articlecomments;
}


public Boolean isDeleted () {
return deleted;
}


public void setdeleted (Boolean deleted) {
this.deleted = deleted;
}


@ManyToOne (fetch = Fetchtype.lazy)
@JoinColumn (name = "path_id")
Public Careerpath Getcareerpath () {
return careerpath;
}


public void Setcareerpath (Careerpath careerpath) {
This.careerpath = Careerpath;
}


@Override
public boolean equals (Object o) {
if (this = = O) {
return true;
}
if (! ( o instanceof article)) {
return false;
}


Final Article Article = (article) o;


Return! (id! = null?!article.equals (article.id): Article.id! = null);


}


/**
* {@inheritDoc}
*/
@Override
public int hashcode () {
return (id! = null? Id.hashcode (): 0);
}


}


The following is a procedure for assigning a value to a custom bean.

list<articlecommentutil> jsonlist = new arraylist<articlecommentutil> ();
list<articlecomment> articlecomments = Articlecommentmanager
. getarticlecomments (ArticleID);
Articlecommentutil Articlecommentutil;
for (Articlecomment articlecomment:articlecomments) {
Articlecommentutil = new Articlecommentutil ();
Articlecommentutil.setid (Articlecomment.getid ());
Articlecommentutil.setcontent (Articlecomment.getcontent ());
Articlecommentutil.setcommenttime (Articlecomment.getcommenttime ());
Articlecommentutil.setcommentator (Articlecomment.getcommentator ()
. GetUserName ());
if (articlecomment.getarticlecomment () = null) {
Articlecommentutil.setreplyer (articlecomment
. Getarticlecomment (). Getcommentator (). GetUserName ());
}
Jsonlist.add (Articlecommentutil);


}



JSON parsing occurs when 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.