在这篇文章中,我们将为您详细介绍net.sf.json.JSONException:Thereisacycleinthehierarchy的内容。此外,我们还会涉及一些关于atitit.解决net.s
在这篇文章中,我们将为您详细介绍net.sf.json.JSONException: There is a cycle in the hierarchy的内容。此外,我们还会涉及一些关于atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy、Exception in thread "main" net.sf.json.JSONException: A JSONArray text must start with '[' at charac、JSON 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
- Exception in thread "main" net.sf.json.JSONException: A JSONArray text must start with '[' at charac
- JSON 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
链接地址:http://www.blogjava.net/peiliangye/articles/372062.html
jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy!
遇到了一些问题,如hibernate延迟加载错误,这都是老掉牙的问题了,一看就知道加个lazy=flase就OK了。想不到快要完成了又遇到了新的问题,JSON死循环,实在让人郁闷。异常如下:
net.sf.json.JSONException: There is a cycle in the hierarchy!
at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.
handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
at net.sf.json.JSONObject._fromBean(JSONObject.java:674)
at net.sf.json.JSONObject.fromObject(JSONObject.java:181)
at net.sf.json.JSONArray._processValue(JSONArray.java:2381)
at net.sf.json.JSONArray.processValue(JSONArray.java:2412)
Truncated. see log file for complete stacktrace
>
仔细查了一下发现是hibernate主外键关联的错,后来就想下json源代码下来看,发现大费周章都没搞到json源码,还是老办法反编译瞅瞅,发现JSONArray根据判断取得的不同类型调用相应的方法,
if (object instanceof Collection)
return _fromCollection((Collection)object,jsonConfig);
而我从hibernate那得到的是list,所以去调用了_fromCollection方法,而里面的方法发现一个问题:该方法会不断的拆开实体属性,直到没有为止,而我的ContactGroup里有两个属性用于自身关联
private Set contactGroups = new HashSet(0);
private Set contactGroupPersons = new HashSet(0);
也就是说主外键自身关联的是个死循环,那怎么才能不让他出现这种情况呢,应该有个配置的参数后者终止循环的地方吧,查看发
现,jsonConfig,呵呵,config应该是配置参数吧,参看JsonConfig看见巨多的属性,有点晕PropertyFilter
,不提了,看了老半天,发现了一个属性PropertyFilter,PropertyFilter 是一个interface,代码如下:
public interface PropertyFilter
{
public abstract boolean apply(Object obj,String s,Object obj1);
}
也就是说我可以通过这个方法过滤掉List里的相应属性,只要让它返回true就可过滤掉,……,有点悬……我们重写一下这个方法:
JsonConfig cfg = new JsonConfig();
cfg.setJsonPropertyFilter(new PropertyFilter()
{
public boolean apply(Object source,String name,Object value) {
if(name.equals("contactGroups")||name.equals("contactGroupPersons")) {
return true;
} else {
return false;
}
}
});
将hibernate产生的实体bean中的contactGroups和contactGroupPersons过滤掉就OK了!
然后调用JSONArray.fromObject(mychildren,cfg); mychildren是hibernate返回的list。
2 .ShoppingCartTable(shoppingCart);
3 // 先过滤对set集合的拆解
4 JsonConfigconfig= newJsonConfig();
5 config.setJsonPropertyFilter( newPropertyFilter() {
6@Override
7publicbooleanapply(Objectarg0,Stringarg1,Objectarg2){
8if(arg1.equals("shoppingCarts")){
9returntrue;
10}else{
11false;
12}
13}
14});
15 将数据转换成Json数据 16 JSONArrayjsonObject=JSONArray.fromObject(listCarts,config);
17 System.out.println(jsonObject.toString());
18
atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy
atitit.解决net.sf.json.JSONExceptionThereisacycleinthehierarchy
1.环境:使用hibernate4跟个,要不个哪的对象系列化成个json的时候儿有这个问题了... 1
2.原因::hb默认的lazy方式造成的当有关联对象的时候儿... 1
3.#---解决::lazy=false(推荐).. 1
4.别的有以下的四个方法可以解决hibernate的序列化问题 2
5.BeanUtils.copyProperties可以解决... 2
6.属性过滤器PropertyFilter 2
7.简单属性过滤器setExclusions法 3
8.JsonBeanProcessor法 4
9.设置JSON-LIB的setCycleDetectionStrategy属性让其自己处理循环(ati测试不生效O81).. 4
10.参考 5
1.环境:使用hibernate4跟个,要不个哪的对象系列化成个json的时候儿有这个问题了...
2.原因::hb默认的lazy方式造成的当有关联对象的时候儿...
3.#---解决::lazy=false(推荐)..
<!--o7oati-->
<!--many开头的是代表该表持有外键-->
<!--class可以不写,因为根据name的值computer(属性),会通过反射自动找到属于哪个类的-->
<many-to-onename="prgrm"insert="false"update="false"lazy="false">
<columnname="progarmme_id"/>
</many-to-one>
作者::老哇的爪子Attilax艾龙,EMAIL:1466519819@qq.com
4.别的有以下的四个方法可以解决hibernate的序列化问题
1domain类实现JSONString接口
2建立JsonConfig实例,并配置属性排除列表
3用属性过滤器
4写一个自定义的JsonBeanProcessor
5.BeanUtils.copyProperties可以解决...
GvProgrammestp=(GvProgramme)arg1;
GvProgrammeo=newGvProgramme();
BeanUtils.copyProperties(o,stp);
6.属性过滤器PropertyFilter
//先过滤对set集合的拆解4JsonConfigconfig=newJsonConfig();5config.setJsonPropertyFilter(newPropertyFilter(){6@Override7publicbooleanapply(Objectarg0,Stringarg1,Objectarg2){8if(arg1.equals("shoppingCarts")){9returntrue;10}else{11returnfalse;12}13}14});15//将数据转换成Json数据16JSONArrayjsonObject=JSONArray.fromObject(listCarts,config);17System.out.println(jsonObject.toString());
7.简单属性过滤器setExclusions法
2.第二种方法通过jsonconfig实例,对包含和需要排除的属性进行方便添加删除
[java]viewplaincopyprint?
5publicclassperson{<br>
6privateStringname;<br>
7privateStringlastname;<br>
8privateAddressaddress;<br>
9<br>
10//getters&setters<br>
11}<br>
12<br>
13JsonConfigjsonConfig=newJsonConfig();<br>
14jsonConfig.setExclusions(newString[]{"address"});<br>
15Personbean=/*initialize*/;<br>
16JSOnjson=JSONSerializer.toJSON(bean,jsonConfig);
注意:这种方法不区分目标类,就是说如果有2个bean当中都存在“address”属性,那么采用这种方法,这两个bean中的address属性都将被排除
3.使用propertyFilter可以允许同时对需要排除的属性和类进行控制,这种控制还可以是双向的,也可以应用到json字符串到java对象
publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse){
LibtypeDAOlibtypeDAO=newLibtypeDAO();
List<Libtype>list=libtypeDAO.findAll();
JsonConfigjsonConfig=newJsonConfig();//建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false);//设置默认忽略
jsonConfig.setExcludes(newString[]{"libs"});//此处是亮点,只要将所需忽略字段加到数组中即可,在上述案例中,所要忽略的是“libs”,那么将其添到数组中即可,在实际测试中,我发现在所返回数组中,存在大量无用属性,如“multipartRequestHandler”,“servletWrapper”,那么也可以将这两个加到忽略数组中.
8.JsonBeanProcessor法
4.最后来看JsonBeanProcessor,这种方式和实现JsonString很类似,返回一个代表原来的domain类的合法JSONOBJECT
1.设置JSON-LIB让其过滤掉引起循环的字段:
9.设置JSON-LIB的setCycleDetectionStrategy属性让其自己处理循环(ati测试不生效O81)..
2.
3.省事但是数据过于复杂的话会引起数据溢出或者效率低下。
publicActionForwardexecute(ActionMappingmapping,HttpServletResponseresponse){
LibtypeDAOlibtypeDAO=newLibtypeDAO();
List<Libtype>list=libtypeDAO.findAll();
JsonConfigjsonConfig=newJsonConfig();//建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false);//设置默认忽略
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);//此处是亮点,不过经过测试,第2种方法有些悲剧,虽然可以使用,但其结果貌似循环数次,至于为啥,还请高人指点。
JSONArrayjsonArray=JSONArray.fromObject(list,jsonConfig);//加载配置文件
returnnull;
}
3.
10.参考
关于json-libThereisacycleinthehierarchy!问题的3种解决办法_威廉庄园_williambryantliu的和讯博客.htm
使用json-lib完成json的序列化和反序列化-话别深秋-博客频道-CSDN.NET.htm
hibernate中lazy的使用-TOYOE-博客园.htm
Exception in thread "main" net.sf.json.JSONException: A JSONArray text must start with '[' at charac
原文链接:http://www.jb51.cc/article/p-kdfmewvc-zw.html
使用高级文本编辑器转换为 无 BOM编码格式即可。
有BOM和无BOM效果图:链接:http://www.jb51.cc/article/p-mswqefxb-zk.html
json解析报错A JSONArray text must start with ''[''
这里使用的是Android系统的,数据格式是正确的
- JSONArrayjoa=newJSONArray(data);
解析代码就这一句 (数据格式是对,是对的,对的)问了好多人都说格式是不对,要是这问题怎么可能老去问了是吧
百度了好久也是什么格式问题啊 什么解析包导错了啦等等啊。。
昨天弄了一天 没有解决。谷歌了这样一条线索 可能数据编码影响,看返回的结果貌似是utf-8的,我又去看我下载数据的地方
copy
JSON There is a cycle in the hierarchy异常
JSON There is a cycle in the hierarchy异常 博客分类: java 异常在使用 pg数据库时,对象转换成json字符串报这个异常。原因是org.postgresql.jdbc4.Jdbc4Array 对象中有这个属性,json 不好处理。
json 报错 There is a cycle in the hierarchy!
因为项目是使用的 hibernate 的, 所以查数据库里面查询的时候,会 join 查询到 其他对象的。 如果是 使用 jackjson 外国的 json 去 放入 这个查询 对象的话。 那么 就会报错:
There is a cycle in the hierarchy!
当然了, 是循环引用什么的鬼的错误, 又不是什么复杂的情况,这个问题,是个 bug 吧。 网上百度也是挺多的,,,看了一下,看不明白,而且挺麻烦的。 然后我就 换 成 阿里 的 fastjson 去使用就没有这个问题了的。 搞不懂,阿里的 json 挺好用的, 其他有些人就是 喜欢 jackjson . 阿里的 json 果然 牛 B,点赞。
可是上面的只是 单元测试可用而已。如果是 输入 web 前端的话, 还是会报错的。
No serializer found for class org.hibernate.proxy.pojo.javassist.Javassi
加上 即可:
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "user", "auditerUser" })
这里 mobel 关联了 user 对象 和 auditerUser 。
关于net.sf.json.JSONException: There is a cycle in the hierarchy的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy、Exception in thread "main" net.sf.json.JSONException: A JSONArray text must start with '[' at charac、JSON There is a cycle in the hierarchy异常、json 报错 There is a cycle in the hierarchy!等相关内容,可以在本站寻找。
本文标签: