首页 > 学院 > 开发设计 > 正文

JSON 自定义序列化(过滤掉不想要的属性)

2019-11-08 01:44:15
字体:
来源:转载
供稿:网友
    这里只讲JSON-LIB和FastJson两种Json工具的序列化过滤问题    比如,我想过滤的属性有id,和name,先演示json-lib    1.Json-Lib        //通过json-lib将数据序列化为json格式        JsonConfig jsonConfig = new JsonConfig();        //进行过滤        jsonConfig.setExcludes(new String[]{"id","name"});        //转化成json数据格式        JSONObject jsonObject = new JSONObject();        String json = jsonObject.toString();        //将json数据通过流写入到客户端        HttpServletResponse response = ServletActionContext.getResponse();        response.setContentType("text/json;charset=UTF-8");        response.getWriter().PRint(json);    2.FastJson                //使用fastjson,过滤        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();        //过滤字段        Set<String> set = filter.getExcludes();        set.add("id");        set.add("name");        String jsonString = JSON.toJSONString(pageBean,filter);        //处理中文乱码问题!        HttpServletResponse response = ServletActionContext.getResponse();        response.setContentType("text/json;charset=UTF-8");        response.getWriter().print(jsonString);    第一次写blog,知识的积累,贵在点滴!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表