net.sf.json.JSONException: There is a cycle in the hierarchy
来源:互联网 发布:易语言qq炫舞辅助源码 编辑:程序博客网 时间:2024/05/21 09:30
原文地址IT宅
当使用json-lib在Java中把对象转换为JSON字符串时易产生的错误,主要的原因是出现了如下的情形:
model a里面包含了b,而model b里面又包含了a,这样造成了解析成对象的过程中的死循环,于是就报错了:
net.sf.json.JSONException: There is a cycle in the hierarchy! at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
如果还是需要保留这种关系,可以使用json-lib提供的JsonConfig把循环的属性在转换的过程中忽略掉
JsonConfig config = new JsonConfig();config.setExcludes(new String[]{"department"});String json = JSONArray.fromObject(userList, config).toString();
当然,还有如下的方法可以实现属性的排除:
实现JSONString接口:(排除了desc)
public class User implements JSONString { private String name; private String password; private String desc; // getters & setters public String toJSONString() { return "{name:'"+name+"', password:'"+password+"'}"; <span style="color:#ff9900;"> //也可以这样 JSONObject json = new JSONObject(); json.put("specialProjectName", specialProject==null?"":specialProject.getName()); return json.toString();</span> } }设置JsonConfig的propertyFilter过滤属性:
public class User { private String name; private String password; private String desc; // ... } JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter( new PropertyFilter(){ public boolean apply( Object source, String name, Object value ){ return source instanceof User && name.equals("desc"); } }); User user = new User(); JSON json = JSONSerializer.toJSON( user, jsonConfig )
写一个自定义的JsonBeanProcessor方法:
public class User { private String name; private String password; private String desc; // ... } JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonBeanProcessor( User.class, new JsonBeanProcessor(){ public JSONObject processBean( Object bean, JsonConfig jsonConfig ){ if( !(bean instanceof User) ){ return new JSONObject(true); } User user = (user) bean; return new JSONObject() .element( "name", user.getName() ) .element( "password", user.getPassword() ); } }); User user = new User(); JSON json = JSONSerializer.toJSON( user, jsonConfig );
0 0
- 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!的解决
- Json解析时出现net.sf.json.JSONException: There is a cycle in the hierarchy!42
- 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!
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy异常,解决方法
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy异常的解决方法
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
- net.sf.json.JSONException: There is a cycle in the hierarchy
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy! 异常
- 学习Markdown
- 选项卡(TabHost)的功能和用法
- Matlab求方差,均值
- matlab图像分割算法源码
- Android 深入ViewPager实现类京东商城首页广告效果
- net.sf.json.JSONException: There is a cycle in the hierarchy
- Hadoop计算中的Shuffle过程
- 转换到 COFF 期间失败: 文件无效或损坏
- Maven java web 项目 单元测试 报 class not found 错误
- Linux设备驱动(三)块设备
- Openstack Nova(四)----Instance 创建(Overview)
- 将博客搬至CSDN
- 接口隔离原则
- IOS App Icon Size 图标尺寸