改良版的json-lib2.4
大家都知道jsonlib2.4之后有了属性过滤的功能,就是当把一个bean转成json的时候能指定哪些属性输出哪些不输出,不过代码很难看,如下 JsonConfig config = new JsonConfig(); config.setJsonPropertyFilter(new PropertyFilter(){ public boolean apply(Object source, String name, Object value) { if(name.equals("parentGroup") || name.equals("childGroups")) { return true; } else { return false; } } }); Iouser user = (Iouser) getBaseManager().get(Iouser.class, iouserId); JSONObject jsonObject = JSONObject.fromObject(user, config); 改良后新加了类MyFilter,下面是实例: util方法: public static String listToJsonArray(List<?> rs, PropertyFilter fs) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(fs); JSONArray json = JSONArray.fromObject(rs, jsonConfig); return json.toString(); } 调用util: MyJsonUtil.listToJsonArray(list, new MyFilter("count", "pn", "url", "title")) *这样写的话,是输出你指定的属性。 MyJsonUtil.listToJsonArray(list, new MyFilter(false, "count", "pn", "url", "title")) *这样写的话,是不输出你指定的属性。 不好意思没写注释。。大家可以反编译了看看。这个改良包我用了一年多了,源码找不着了。好像就新加了这一个类。最近缺分了,发上来攒点。
- wenwawa882013-10-06主要是jquery不少插件使用json数据格式,所以下载下来再java中封装json数据,谢谢了,还不错
- 粉丝: 0
- 资源: 6
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助