前一片我简单说了json格式数据与对象直接的转换。
但是有时候你会发现,出问题了,数据格式化不了,还报了错误,怎么回事?
以下是我出现的错误:
Exception in thread "main" 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:833)
at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
at net.sf.json.JSONArray._processValue(JSONArray.java:2514)
at net.sf.json.JSONArray.processValue(JSONArray.java:2539)
at net.sf.json.JSONArray.addValue(JSONArray.java:2526)
at net.sf.json.JSONArray._fromCollection(JSONArray.java:1057)
at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:237)
at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
at net.sf.json.JSONObject.fromObject(JSONObject.java:130)
at test.Test2.main(Test2.java:28)
一开始的时候我也疑惑是怎么回事,之前的都可以正常格式化,为什么这样不行,后来在别人的指导下检查了代码,发现因为我使用了自身关联(我这样叫他)。
代码如下:
package test;
import java.util.List;
public class ClassS {
//id
private int id ;
//班级名称
private String name;
//拥有的学生
private List<Student> students;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public ClassS(int id, String name, List<Student> students) {
super();
this.id = id;
this.name = name;
this.students = students;
}
}
package test;
public class Student {
//id
private int id ;
//名称
private String name;
//年龄
private int age;
//性别
private String gender;
//住址
private String address;
//身份证id
private String cdCard;
//所属班级
private ClassS classs;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCdCard() {
return cdCard;
}
public void setCdCard(String cdCard) {
this.cdCard = cdCard;
}
public ClassS getClasss() {
return classs;
}
public void setClasss(ClassS classs) {
this.classs = classs;
}
public Student(int id, String name, int age, String gender, String address,
String cdCard) {
super();
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
this.cdCard = cdCard;
}
}
public static void main( String[] args ){
Student stu1 = new Student(1, "goodboy",20,"boy","我的老家在东北,家住松花江上啊", "123456789123456789");
Student stu2 = new Student(2, "goodboy",20,"boy","我的老家在东北,家住松花江上啊", "123456789123456789");
List<Student> stus = new ArrayList<Student>();
stus.add(stu1);
stus.add(stu2);
ClassS classs = new ClassS(1,"class1",stus);
stu1.setClasss(classs);
stu2.setClasss(classs);
System.out.println(JSONObject.fromObject(classs));
}
从上面看出代码都是正确的,但是你会发现在ClassS与Student是一对多的关系,及一个班级对应了多个学生,而在Student中又对应了所属的班级:
//所属班级
private ClassS classs;
结果json在格式化ClassS 这个对象的时候在进行格式化其内部的对象时候,由于json是深度递归进行格式化的又回到了其自身,结果就成了一个死循环,问题就出现了。既然有问题肯定要解决,怎么解决呢?我的方法是过滤掉其自身的关联,json的JSONObject和JSONArray都提供了这个过滤的机制:
JSONObject.fromObject(object, jsonConfig)
JSONArray.fromObject(object, jsonConfig)
所以我自己写了一个过滤的类:
package test;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
/**
*
* 功能描述:过滤掉格式化数据为json格式的时候的自身关联
*
*/
public class FiltrateUtil {
public static JsonConfig filtrate(final String[] str) {
JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if(str!=null&&str.length>0){
boolean flag = false;
for(int i = 0;i<str.length;i++){
if (name.equals(str)) {
flag = true;
break;
}
}
return flag;
}else{
return false;
}
}
});
return config;
}
}
然后在把测试的代码修改下,如下:
System.out.println(JSONObject.fromObject(classs,FiltrateUtil.filtrate(new String[]{"classs"})));
我想要的json字符就正常的被打印出来了。
{"id":1,"name":"class1","students":[{"address":"我的老家在东北,家住松花江上啊","age":20,"cdCard":"123456789123456789","gender":"boy","id":1,"name":"goodboy"},{"address":"我的老家在东北,家住松花江上啊","age":20,"cdCard":"123456789123456789","gender":"boy","id":2,"name":"goodboy"}]}