解决json,There is a cycle in the hierarchy,只循环到某一个关联层

来源:互联网 发布:java双线程 编辑:程序博客网 时间:2024/04/29 10:01

今天使用java解析json遇到There is a cycle in the hierarchy错误,也就是类与类之间关联,http://blog.csdn.net/jazywoo123/article/details/8681555


JSONArray根据判断取得的不同类型调用相应的方法,

if (object instanceof Collection)
    return _fromCollection((Collection)object, jsonConfig);

而我从hibernate那得到的是list,所以去调用了_fromCollection方法,而里面的方法发现一个问题:该方法会不断的拆开实体属性,直到没有为止

package bijian.model.bean;import java.util.Date;import java.util.HashSet;import java.util.Set;public class User{   private long userID;   private String username;   private String nickname;   private String password;   private Integer sex;   private Integer age;   private String photo;      private Date createTime;    private Integer loginState;   private Integer hotValue;   private Integer attentionNum;   private Integer followingNum;   private Integer sentenceNum;   private Integer visitNum;      private Set attentions=new HashSet();   private Set followings=new HashSet();   private Set friends=new HashSet();      private Set chats=new HashSet();   private Set notices=new HashSet();   private Set messages=new HashSet();      private Set sentences=new HashSet();   private Set receiveComments=new HashSet();   private Set sendComments=new HashSet();   private Set reportSentences=new HashSet();   private Set relatedSentences=new HashSet();   private Set loveSentences=new HashSet();      private Set labelUsers=new HashSet();   private Set subscribeLabels=new HashSet();   ......................................}

package bijian.model.bean.relationbean;import java.util.Date;import bijian.model.bean.User;public class Attention {    private long attentionID;    private User self;    private User attentioner;    private Date createTime;    private Integer isValid;    ..........................}

可以看出Attention类  多对一  User类,如果有

List<Attention> attentions=(List<Attention>) resultMap.get("ownPage_ownAttentionList");

JASONArray.fromObject(attentions) 就会报错 ,具体参见http://blog.csdn.net/jazywoo123/article/details/8681555

但是我想要保留attention中的User属性,但是不过滤User中其他的属性,即

[{"attentionID":1,      "attentioner":{"age":12,"attentionNum":2,"createTime":null,"followingNum":0,"hotValue":34,"loginState":0,.......................................

attentioner是一个User对象,但是User对象中的那些复杂的对象属性过滤掉。

可以看到JASONArray的方法中JsonConfig是一层层传下去的,即过滤完Attention之后,又使用在User转json中

if (object instanceof Collection)
    return _fromCollection((Collection)object, jsonConfig);

因此,可以在过滤Attention的属性中加上User的属性

 private JsonConfig jsonFilterProperty(final List<String> properties){    JsonConfig config=new JsonConfig();    config.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());    if(properties!=null&&properties.size()>0){    config.setJsonPropertyFilter(new PropertyFilter() {    public boolean apply(Object object, String name, Object value) {    if(containsElement(properties,name)){    //System.out.println("-- 过滤  "+name+"----");    return true;    }    return false;    }    });    }    return config;    }    private <T> boolean containsElement(List<T> list,Object object){    for(T l:list){    if(l.toString().equals(object.toString()) ){    return true;    }    }    return false;    }    //得到root命名下的List的json,过滤掉properties中的字段    private <T> String getJson(String root,List<T> list,List<String> properties){    JsonConfig config=jsonFilterProperty(properties);    JSONArray sentenceListJson=JSONArray.fromObject(list,config);    JSONObject jsonObject=new JSONObject();    jsonObject.put(root, sentenceListJson);    return jsonObject.toString();    }    //输出json    private <T> String printJson(String root,List<T> list,String className){    if(list==null) return null;    String resultJson = null;    if(list.size()>0){    List<String> properties=new ArrayList<String>();    if(className.equals("User")){    addProperty(properties,className);    }else if(className.equals("Sentence")){    addProperty(properties,className);    }else if(className.equals("Label")){    addProperty(properties,className);    }else if(className.equals("Attention")){    addProperty(properties,"User");    }else if(className.equals("Following")){    addProperty(properties,"User");    }else if(className.equals("SubscribeLabel")){    addProperty(properties,"Label");    addProperty(properties,"User");    }else if(className.equals("LoveSentence")){    addProperty(properties,"User");    addProperty(properties,"Sentence");    }        resultJson=getJson(root, list, properties);        System.out.println(resultJson);    }    return resultJson;    }         private void addProperty(List<String> properties,String className){    if(className.equals("User")){  //生成json时过滤User的这些属性,以免产生循环错误properties.add("attentions");        properties.add("followings");        properties.add("friends");        properties.add("chats");        properties.add("notices");        properties.add("messages");        properties.add("sentences");        properties.add("receiveComments");        properties.add("sendComments");        properties.add("reportSentences");        properties.add("relatedSentences");        properties.add("loveSentences");        properties.add("labelUsers");        properties.add("subscribeLabels");}else if(className.equals("Sentence")){//生成json时过滤Sentence的这些属性,以免产生循环错误properties.add("author");        properties.add("comments");        properties.add("labelSentences");}else if(className.equals("Label")){//生成json时过滤Label的这些属性,以免产生循环错误properties.add("labelSentences");        properties.add("labelUsers");        properties.add("subscribeLabels");}    }
这样之后,调用,即可让Attention转json进行到User之后不往下循环
List<Attention> attentions=(List<Attention>) resultMap.get("ownPage_ownAttentionList");        printJson("attentionList", attentions, Attention.class.getSimpleName());