Json-lib的使用小结

以前在使用Json-lib的时候,总是报
There is a cycle in the hierarchy!
这个错误,所以好长时间使用json数据的时候都是在Action中自己手工装配json格式的数据,前几天,在JE上面看到这篇帖子: http://www.iteye.com/topic/826988这篇帖子,看到这个链接 http://s.xnimg.cn/a13819/allunivlist.js,这个链接上是JSON格式的数据,貌似这些数据不少,但是,如果手工装配的话,那肯定是一个累人的活,就算是用循环(PS:俺不知道JSON-lib上面是怎么做的,以后有时间再研究研究),所以自然就想到了json-lib(PS:貌似还有GSON,Google的,据说效率老高了,但是struts2的lib中是json-lib),但是俺又不咋会用json-lib,所以只能求助于大家了,在JE上提了一个问题 http://www.iteye.com/problems/56669(再次感谢大家的帮助),所以总结一下:
这里,模仿人人网上,建立三个实体:Province,College,Department,分别是省份,大学,院系,关联关系如下:
Province-----College一对多,双向
College------Department一对多,双向

实体的主要代码如下:
public class Province {
	/*省份id*/
	private Integer proid;
	/*省份名*/
	private String proname;
	
	private Set<College> colleges = new HashSet<College>();
}

public class College {
	/*学校id*/
	private Integer colid;
	/*学校名称*/
	private String name;
	/*学校所在省*/
	private Province province;
	private Set<Department> depts = new HashSet<Department>();
}

public class Department {
	private Integer depid;
	
	private String depname;
	
	private College college;
}

以我个人的理解,测试代码如下:
@Test
	public void testFormat() {
		Province pro = new Province();
		pro.setProid(12);
		pro.setProname("湖北省");
		College col1 = new College();
		col1.setColid(1);
		col1.setName("黄冈师范");
		col1.setProvince(pro);
		Department dept1 = new Department();
		dept1.setDepid(11);
		dept1.setDepname("计科院");
		Department dept2 = new Department();
		dept2.setDepid(12);
		dept2.setDepname("计科");
		Department dept3 = new Department();
		dept3.setDepid(13);
		dept3.setDepname("计科");
		Set<Department> depts = new HashSet<Department>();
		depts.add(dept1);
		depts.add(dept2);
		depts.add(dept3);
		col1.setDepts(depts);
		
		College col2 = new College();
		col2.setColid(2);
		col2.setName("黄冈师范学院");
		col2.setProvince(pro);
		Department dept4 = new Department();
		dept4.setDepid(21);
		dept4.setDepname("计科院");
		Department dept6 = new Department();
		dept6.setDepid(22);
		dept6.setDepname("计科");
		Department dept5 = new Department();
		dept5.setDepid(23);
		dept5.setDepname("计科");
		Set<Department> depts2 = new HashSet<Department>();
		depts2.add(dept4);
		depts2.add(dept5);
		depts2.add(dept6);
		col2.setDepts(depts2);
		
		Set<College> cols = new HashSet<College>();
		cols.add(col1);
		cols.add(col2);
		pro.setColleges(cols);
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new CustomPropertyFilter());
		JSONObject json = JSONObject.fromObject(pro, config);
		System.out.println(json.toString());
	}

但是打印出来的数据是:
{"colleges":[{"colid":1,"depts":[{"college":null,"depid":13,"depname":"计科"},{"college":null,"depid":12,"depname":"计科"},{"college":null,"depid":11,"depname":"计科院"}],"name":"黄冈师范","province":null},{"colid":2,"depts":[{"college":null,"depid":21,"depname":"计科院"},{"college":null,"depid":22,"depname":"计科"},{"college":null,"depid":23,"depname":"计科"}],"name":"黄冈师范学院","province":null}],"proname":"湖北省","proid":12}

打印出来的json数据中有"college":null和"province":null这样的数据,所以自然不爽,看了 大虾的回答,就加上了类:
import net.sf.json.util.PropertyFilter;

/**
 * 自定义属性过滤器
 * 
 * @author qiuzj
 *
 */
public class CustomPropertyFilter implements PropertyFilter {

	@Override
	public boolean apply(Object source, String name, Object value) {
		// 过滤掉为null的属性
		if (value == null)
			return true;
		return false;
	}

}

然后在测试的代码中加上如下代码:
JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new CustomPropertyFilter());
JSONObject json = JSONObject.fromObject(pro, config);

这样就可以去掉json数据中为null(PS:上面的测试代码中College类中的Province属性没有设置,Department类中的College属性没有设置)。
    如果要设置College类中的Province属性,则会报
There is a cycle in the hierarchy!
错误,所以应该再加上:
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
这样就不会报错(但是这样加上之后,又会出现"province":null的情况,解决办法俺还不知道)
对于JSON-lib大家还有什么更好的办法,大家一起研究一下.......

你可能感兴趣的:(Json-lib的使用小结)