遇到了一些问题,如hibernate延迟加载错误,这都是老掉牙的问题了,一看就知道加个lazy=flase就OK了。想不到快要完成了又遇到了新的问题,JSON死循环,实在让人郁闷。异常如下:
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:674)
at net.sf.json.JSONObject.fromObject(JSONObject.java:181)
at net.sf.json.JSONArray._processValue(JSONArray.java:2381)
at net.sf.json.JSONArray.processValue(JSONArray.java:2412)
Truncated. see log file for complete stacktrace
>
仔细查了一下发现是hibernate主外键关联的错,后来就想下json源代码下来看,发现大费周章都没搞到json源码,还是老办法反编译瞅瞅,发现JSONArray根据判断取得的不同类型调用相应的方法,
if (object instanceof Collection)
return _fromCollection((Collection)object, jsonConfig);
而我从hibernate那得到的是list,所以去调用了_fromCollection方法,而里面的方法发现一个问题:该方法会不断的拆开实体属性,直到没有为止,而我的ContactGroup里有两个属性用于自身关联
private Set contactGroups = new HashSet(0);
private Set contactGroupPersons = new HashSet(0);
也就是说主外键自身关联的是个死循环,那怎么才能不让他出现这种情况呢,应该有个配置的参数后者终止循环的地方吧,查看发
现,jsonConfig,呵呵,config应该是配置参数吧,参看JsonConfig看见巨多的属性,有点晕PropertyFilter
,不提了,看了老半天,发现了一个属性PropertyFilter,PropertyFilter 是一个interface,代码如下:
public interface PropertyFilter
{
public abstract boolean apply(Object obj, String s, Object obj1);
}
也就是说我可以通过这个方法过滤掉List里的相应属性,只要让它返回true就可过滤掉,……,有点悬……我们重写一下这个方法:
JsonConfig cfg = new JsonConfig();
cfg.setJsonPropertyFilter(new PropertyFilter()
{
public boolean apply(Object source, String name, Object value) {
if(name.equals("contactGroups")||name.equals("contactGroupPersons")) {
return true;
} else {
return false;
}
}
});
将hibernate产生的实体bean中的contactGroups和contactGroupPersons过滤掉就OK了!
然后调用JSONArray.fromObject(mychildren,cfg); mychildren是hibernate返回的list。
2 .ShoppingCartTable(shoppingCart);
3 // 先过滤对set集合的拆解
4 JsonConfig config = new JsonConfig();
5 config.setJsonPropertyFilter( new PropertyFilter() {
6 @Override
7 public boolean apply(Object arg0, String arg1, Object arg2) {
8 if (arg1.equals("shoppingCarts")) {
9 return true;
10 } else {
11 return false;
12 }
13 }
14 });
15 // 将数据转换成Json数据
16 JSONArray jsonObject = JSONArray.fromObject(listCarts, config);
17 System.out.println(jsonObject.toString());
18
根据我在网上查找的资料,解决办法有如下3种:
1.设置JSON-LIB让其过滤掉引起循环的字段:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
LibtypeDAO libtypeDAO = new LibtypeDAO();
List<Libtype> list = libtypeDAO.findAll();
JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
jsonConfig.setExcludes(new String[]{"libs"}); //此处是亮点,只要将所需忽略字段加到数组中即可,在上述案例中,所要忽略的是“libs”,那么将其添到数组中即可,在实际测试中,我发现在所返回数组中,存在大量无用属性,如“multipartRequestHandler”,“servletWrapper”,那么也可以将这两个加到忽略数组中.
JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig); //加载配置文件
return null;
}
2.设置JSON-LIB的setCycleDetectionStrategy属性让其自己处理循环,省事但是数据过于复杂的话会引起数据溢出或者效率低下。
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
LibtypeDAO libtypeDAO = new LibtypeDAO();
List<Libtype> list = libtypeDAO.findAll();
JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); //此处是亮点,不过经过测试,第2种方法有些悲剧,虽然可以使用,但其结果貌似循环数次,至于为啥,还请高人指点。
JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig); //加载配置文件
return null;
}
3.最为原始的办法,自己写个JavaBean,用forEach循环,添加到List中,这个方法我看网上有人成功,我没试,但大概过程可以写出来,其结果正确性有待检验。
JavaBean:
public LibtypeForm{
int ltid;
string ltname;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
LibtypeDAO libtypeDAO = new LibtypeDAO();
List<Libtype> list = libtypeDAO.findAll();
List<LibtypeForm> formList = new ArrayList();
for(Libtype libtype : list){
LibtypeForm form = new LibtypeForm();
form.setLtid(libtype .getLtid);
form.setLtname(libtype.getLtname);
formList.add(form);
}
JSONArray jsonArray = JSONArray.fromObject(formList);
return null;
}