net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法
使用Hibernate manytoone属性关联主表的时候,如果使用JSONArray把pojo对象转换成json对象时,很容易出现循环的异常。解决的办法就是,
在转换json对象时忽略manytoone属性的对象。比如student类中有course属性,忽略course属性,就不会再出错。
当pojo对象中存在时间(Date)类型时,转换成json后的对象,时间格式不符合使用要求。在转换时,可以定义时间的格式。
public String getJson(List<Student> list){
JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
jsonConfig.setExcludes(new String[]{"course"});
// 设置javabean中日期转换时的格式
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonDateValueProcessor("yyyy-MM-dd"));
JSONArray json=JSONArray.fromObject(list,jsonConfig);
return json;
}
时间格式转换辅助类
package org.jeecgframework.web.ksxdcgzh.entity; import java.text.SimpleDateFormat;
import java.util.Date; import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor; public class JsonDateValueProcessor implements JsonValueProcessor{
private String format = "yyyy-MM-dd HH:mm:ss"; public JsonDateValueProcessor()
{
} public JsonDateValueProcessor(String format)
{
this.format = format;
} @Override
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
// TODO Auto-generated method stub
String[] obj = {};
if (value instanceof Date[])
{
SimpleDateFormat sf = new SimpleDateFormat(format);
Date[] dates = (Date[]) value;
obj = new String[dates.length];
for (int i = 0; i < dates.length; i++)
{
obj[i] = sf.format(dates[i]);
}
}
return obj;
} @Override
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
// TODO Auto-generated method stub
if (value instanceof Date)
{
String str = new SimpleDateFormat(format).format((Date) value);
return str;
}
return value;
}
public String getFormat()
{
return format;
} public void setFormat(String format)
{
this.format = format;
} }
2

在一对多的实体关系中,转换json时要忽略引用的属性。
@Entity
@Table(name = "studentl", schema = "")
@DynamicUpdate(true)
@DynamicInsert(true)
@SuppressWarnings("serial")
@JsonIgnoreProperties(value={"course"}) //转换json时忽略course对象,以免造成死循环
public class StudentEntity implements java.io.Serializable {
private courseEntity course;
......
}
net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法的更多相关文章
- atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy
atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy 1. 环境:使用hibernate4跟个,,要不个哪的对象系列 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!
因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案 今天在用List集合转换成json数组的时候发生了这个错误,这个 ...
- json-lib-2.4-jdk15.jar 报错 net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案(Hibernate)
使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy! 转json死循环问题解决
解决上述问题遵照两个原则就可以: 1.页面不需要展示关联数据时 解决:将关联对象属性排除掉 2.页面需要展示关联数据时 解决:将关联对象改为立即加载,并且将关联对象中的属性排除
- Json_异常_net.sf.json.JSONException: JSONObject["solution"] not found.
net.sf.json.JSONException: JSONObject["solution"] not found. 没有这个元素造成的. 问题代码: JSONObject j ...
- net.sf.json.JSONException: java.lang.NoSuchMethodException
在尝试将json对象转换为list时候出现了如下错误 Exception in thread "main" net.sf.json.JSONException: java.lang ...
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- XML2JSON 的【net.sf.json.JSONException: nu.xom.ParsingException must be followed by either attribute specifications, ">" or "/>"】问题解决办法
在使用JSon-Lib库进行XML2JSon的转换时,在JUnit测试时没有什么问题,但是在Tomcat里面跑的时候,抛出了下面的异常,查找了google,发现关于这方便的文章比较少,即使有,也需要F ...
随机推荐
- python随心笔记
print "hello,world" #打印hello,world print "1+1" #打印字符串 1+1 单引号和双引号是单行字符串 print [& ...
- kvm常用操作
安装一些虚拟化的组件 yum -y install kvm python-virtinst libvirt bridge-utils virt-manager qemu-kvm-tools virt- ...
- 图像抠图算法学习 - Shared Sampling for Real-Time Alpha Matting
一.序言 陆陆续续的如果累计起来,我估计至少有二十来位左右的朋友加我QQ,向我咨询有关抠图方面的算法,可惜的是,我对这方面之前一直是没有研究过的.除了利用和Photoshop中的魔棒一样的技术或者 ...
- ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...
- [No00009D]使用visual studio 2015 update3打包程序安装包的简单方法(不需要InstallShield)
注意: 该方法只适用于小型软件的打包发布: 该打包向导可以预先检查需要的运行库支持: 由于visual studio自2012后取消掉了自带的打包程序,如果有需要打包安装,需要使用一个叫用Instal ...
- BZOJ 1001: [BeiJing2006]狼抓兔子
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 20029 Solved: 4957[Submit][ ...
- [LeetCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- Goodbye 2016 总结与展望
今天居然是2016年的最后一天了,写点什么回忆吧. 2016开始的时候我刚拿到普及组一等奖,还只是压线,水平很差.学校并不知道这有多差,于是狠狠宣传这所谓的"光荣事迹".那段时间我 ...
- java日志学习笔记
一.日志家族 Log4j一开始就很强大,在jdk自带日志系统之前,apache就曾经尝试把log4j划为java的一部分,不知为何没能成功,sun还是用了自己很弱的日志系统.为了兼容各个日志系统,ap ...
- Docker安装CentOS
系统环境: 腾讯云公共镜像 CoreOS 7.1 X64 #docker 下载centos镜像docker pull centos #下载centos所有的镜像docker pull ...