zoukankan      html  css  js  c++  java
  • 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将会报错





  • 相关阅读:
    算法演示工具
    1198:逆波兰表达式
    1315:【例4.5】集合的划分
    1192:放苹果
    1191:流感传染
    1354括弧匹配检验
    1331【例1-2】后缀表达式的值
    1307高精度乘法
    1162字符串逆序
    1161转进制
  • 原文地址:https://www.cnblogs.com/babyhhcsy/p/3431128.html
Copyright © 2011-2022 走看看