9.jackjson使用教程

 

1.导入相关jar

        <!-- 10.jackson -->
        <!--10.1 定义了底层的streaming API和实现了Json特性 -->
             <dependency>
                 <groupId>com.fasterxml.jackson.core</groupId>
                 <artifactId>jackson-core</artifactId>
                 <version>2.7.3</version>
           </dependency>
           <!-- 10.2 实现了数据绑定和对象序列化,它依赖于streaming和annotations的包  -->
          <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-databind</artifactId>
                  <version>2.7.3</version>
          </dependency>
          <!-- 10.3 包含了标准的Jackson注解。本文暂不介绍 -->
          <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.7.3</version>
          </dependency>

6bc3b16c91fbfaecd781412d82aec6e0f03.jpg

 

2.jackson常用方法记录

2.1 准备实体类

安装lombok插件

https://my.oschina.net/u/4132381/blog/3054137

63e7d4f39717f650e13d633582157ae8bde.jpg

2.2 对象转json

2.2.1 将所有转json 忽略指定的属性 及null值

 

需求:

fe989ebd1281e780f88c4b75ee796abc5a2.jpg

 

效果:

6d464b9897679b959fed478652bdcc3e8e6.jpg

方式1: 缺点:一个类上定义忽略的字段都会被忽略  别人如果需要就没办法了

@JsonIgnoreProperties({"descr", "other"})//忽略字段 descr, other  other 没有用到 这里只是展示忽略多个字段的方式
@JsonInclude(Include.NON_NULL) //忽略  null字段

7ab92e96d251a7988263501f8d67d485d4d.jpg

方式2:根据需要自己在代码中忽略相应的字段  需要引入json-lib

效果:

d3a2b073640af4b57e35f7a98cc2eb1ae58.jpg

实现方式:

引入:jar

        <!--json-lib依赖 -->
                <dependency>
                    <groupId>net.sf.json-lib</groupId>
                    <artifactId>json-lib</artifactId>
                    <version>2.4</version>
                    <classifier>jdk15</classifier>
                </dependency>

f72d098eec440c9d29cd781c83b12b97572.jpg

 

代码中:

    @RequestMapping("/objectToJson3")
    public void objectToJson3(HttpServletResponse response) throws IOException{
        
        //设置响应编码格式
        response.setContentType("text/html; charset=utf-8");
        
        //------------封装数据------------开始-----------------
        User2 u = new  User2();//
        //u.setId(2l);
        u.setName("李四");
        u.setDescr("jsonson 配合json-lib 使用 ");
        u.setOther("json-lib 忽略 不需要的字段");
        
        //------------封装数据------------结束-----------------
        //--------------------json-lib处理数据--开始------------------------------------------------
        JsonConfig jsonConfig = new JsonConfig();//import net.sf.json.JsonConfig;
        jsonConfig.setExcludes(new String[] {"name","descr"});
        JSONArray jsonArray = JSONArray.fromObject(u, jsonConfig);//import net.sf.json.JSONArray;
        //--------------------json-lib处理数据--结束------------------------------------------------

        ObjectMapper mapper=new ObjectMapper();
        String jsonData = mapper.writeValueAsString(jsonArray);
        
        
        PrintWriter out = response.getWriter();
        out.write(jsonData);
        out.flush();
        out.close();
    }

c6cbf02c39f8da346139f0608dec0ff51fa.jpg

 

2.3 list 转 json

效果

22ca8ac5660776d2fa83ef2e33255ab1ae6.jpg

方式:

    //jackson的核心,通过mapper来进行序列化和反序列化
        ObjectMapper mapper=new ObjectMapper();
        //序列化,write相关方法!write可以是序列化输出多种格式,当然这边最常用就是写成为String返回前端
        String jsonData = mapper.writeValueAsString(list);
        

0b7578f8ceb7f808c7af38f9a3561a983d5.jpg

 

2.4 map 转json

 

179b3fdbeccbb0822251a81a55d306ab741.jpg

 

2.4 json转对象

49d645a76a1243af4aeb562bce446455fb0.jpg

36741b0b33ca79526186cd39ee605414107.jpg

2.5 json转集合

    
        List<User3> resultList = mapper.readValue(jsonData, new TypeReference<List<User3>>(){});

4ffa004aea8a64bee0d484f8ae6e4148c24.jpg

2.6 赋值json转对象

2.6.1 对象中含有对象

 

eb854f9823d612b94cc5e91d7cc153eb106.jpg

 

User4 user4 = mapper.readValue(jsonData, User4.class); // 简单方式

440710e77ca268f961d44e73f01171be26f.jpg

2.6.2   对象的属性是集合

 

447c9b3a05993ff7717efc998bece3fb885.jpg

 

efd924d5c4e0046ec9cf3a10a110339f016.jpg

转载于:https://my.oschina.net/u/4132381/blog/3054170

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值