extjs list转json并过滤不需要的属性/JsonConfig

在和Extjs使用时,往往有list想前台放回json数据,这是就需要将list转换为json前台才能后使用
jsonConfig对象可以有效的过滤掉不需要的字段;使用方法
1、创建jsonConfig对象,注意包是net.sf.json.JsonConfig包下的
2、可以使用setExcludes(new String[]{})添加你想要过滤的字段
3、利用JSONArray可以将list转换为json数据
示例代码为:
  
    /** 
     * 根据sub的id进行查询内容
     * @author chensy
     * @date 2013-11-12
     * @throws Exception
     */

    public void queryFeeBySubId() throws Exception{
        List<AgreementFee> list = agreFeeManager.queryBySubId(agreementSubId);
        if(list!=null && list.size() >0){
            for(int i = 0 ;i < list.size() ; i++){
                AgreementFee agreementFee = list.get(i);
                agreementFee.setAgreementSub(null);
            }
        }
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{
         "agreementSub","creatorId""modifyDate""modifyId","modifyName","createDate","modifyDate","creatorName"
        });
        JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig);
        StringBuffer sb = new StringBuffer();
        sb.append("{totalProperty:").append(list.size()).append(",result:");
        sb.append(jsonArray.toString());
        sb.append("}");
         Struts2Utils.getResponse().getWriter().print( sb.toString());
    }
注意:
在使用时:jsonArray无法进行日期的转换如果书写了日期类型转换对jsonArray是不起作用的;
转换时注意,如果使用了hibernate进行查询,注意懒加载的问题 list中有关联对象,jsonArray将会报错





原文地址:https://www.cnblogs.com/babyhhcsy/p/3431128.html