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 ...
随机推荐
- linux基本知识
1.默认不写端口号就是80端口 127.0.0.1.localhost都代表本机 2.linux下的用户管理: id:可以查看当前用户whoami:查看当前的用户who:看当前已经登录的用户w:也 ...
- HTML5全屏(Fullscreen)API详细介绍
// 整个页面 onclick= launchFullScreen(document.documentElement); // 某个元素 launchFullScreen(document.get ...
- 《一个操作系统的实现》 ubuntu系统环境配置
<一个操作系统的实现> ubuntu系统环境配置 电脑之前已经安装了gcc. 一.nasm安装:sudo apt-get install nasm或官网下载http://sourcefor ...
- 项目实现不同环境不同配置文件-maven profile
最近接触的项目都是在很多地方都落地的项目,需要支持不同的环境使用不同的配置文件.一直以来都以为是人工的去写不同的配置文件,手动的去修改运用的配置文件.感觉自己还是太low呀.maven的使用的还停留在 ...
- wireshark 导出所有filter出来的包
1.Edit->Mark All Displayed 2.File->Export Specified Packets->Marked packets only(选中)
- 关于JSONP
一.JSONP的诞生 1.首先,因为AJAX无法跨域,其次开发者发现,<script>标签的src属性是可以跨域的. 2.把跨域服务器写成调用本地的函数,回调数据回来不就好了. 3.JSO ...
- N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- [LeetCode] Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...