解决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());
- 解决json,There is a cycle in the hierarchy,只循环到某一个关联层
- JSON解析关联类型发生死循环 There is a cycle in the hierarchy!
- JSONException: There is a cycle in the hierarchy!解决Json死循环问题
- json-lib There is a cycle in the hierarchy!
- JSON There is a cycle in the hierarchy异常
- json中net.sf.json.JSONException: There is a cycle in the hierarchy!的解决
- JSON 转换异常 死循环 There is a cycle in the hierarchy
- net.sf.json.JSONException: There is a cycle in the hierarchy! 异常解决
- atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy
- 解决net.sf.json.JSONException: There is a cycle in the hierarchy!
- There is a cycle in the hierarchy
- There is a cycle in the hierarchy !!!
- There is a cycle in the hierarchy
- There is a cycle in the hierarchy
- There is a cycle in the hierarchy!
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy
- Json解析时出现net.sf.json.JSONException: There is a cycle in the hierarchy!42
- java.io.IOException: File /home/hadoop/mapred/system/jobtracker.info could only be replicated to 0 n
- 软件工程专业大二下学期了,路该怎么走?
- [ANDROID入门] Windows环境下下载android源代码(2013.3.17)
- android---JNI基本工作步骤
- Flex progressBar在同一上传功能页面下多次上传中的使用
- 解决json,There is a cycle in the hierarchy,只循环到某一个关联层
- 如何得知某期刊是否被EI收录?
- 做程序5年之后,前途开始十分担忧!该何去何从 ……
- 计算几何专项:UVa 11186
- oracle常见操作语句
- CentOS的Hadoop集群配置(一)
- 在ARM上配置Qt远程访问PostgreSQL
- 排列问题
- 快速排序及其变种算法