首页 > 代码库 > Json解析时出现net.sf.json.JSONException: There is a cycle in the hierarchy!

Json解析时出现net.sf.json.JSONException: There is a cycle in the hierarchy!

原因分析在解析bean时,出现死循环调用,即多个bean之间出现了相互调用.解决方法:将关联关系中实体对象间

 的lazy属性设为false过滤掉bean中引起死循环调用的属性。(两种过滤方式)

                //采用数组的方式过滤关联的实体对象

          JsonConfig jsonConfig = new JsonConfig();

 jsonConfig.setIgnoreDefaultExcludes(false);


 jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
 jsonConfig.setExcludes(new String[] { "articleComment", "article",

 "commentator" });


                //重写apply方法的过滤关联对象

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;
 }

 });

              上面两种方式虽然可以解决问题但是我没却没法获得关联实体对象的属性因为这些关联的实体对象都被过滤了。

             自定义bean(如果你想要关联对象的属性时)

 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 ? 2014 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);
}


}


         

               下面是为自定义的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解析时出现net.sf.json.JSONException: There is a cycle in the hierarchy!