Jackson简单应用

Posted 百撕妳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jackson简单应用相关的知识,希望对你有一定的参考价值。

Jackson官网 http://wiki.fasterxml.com/
Jackson下载 https://github.com/FasterXML/jackson-annotations/wiki

本文代码下载 https://pan.baidu.com/s/1dEHqPlZ

本文介绍Jackson简单应用,希望能对大家有用,操作流程如下:

第一步:创建Maven项目,配置pom.xml。

技术分享
<dependencies>
      <!-- Jackson包 -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.1</version>
    </dependency>
  </dependencies>
View Code

第二步:创建测试文件,功能测试。

Users对象

技术分享
package com.learn.jackson;

public class Users {
    private String uuid;
    private String name;
    private Integer age;
    private Integer sex;
    private String remark;
    
    public Users() {
    }
    
    public Users(String uuid,String name,Integer age, Integer sex, String remark){
        this.uuid = uuid;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.remark = remark;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
    
    public String getMassages(){
        return uuid+"--"+name+"--"+sex+"--"+age+"--"+remark;
    }
}
View Code

测试文件

技术分享
package com.learn.jackson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import junit.framework.TestCase;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class AppTest extends TestCase {
    public static void main(String[] args) throws IOException {
        //测试数据
        Users user = new Users("aaa", "aaa", 23, 1, "这是aaa");
        List<Users> list = new ArrayList<Users>();
        list.add(new Users("bbb", "bbb", 45, 1, "这是bbb"));
        list.add(new Users("ccc", "ccc", 23, 1, "这是ccc"));
        //Jackson对象
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        
        System.out.println("********JSON -> 对象************");
        String jsonUser = mapper.writeValueAsString(user);
        String jsonList = mapper.writeValueAsString(list);
        System.out.println(jsonUser);
        System.out.println(jsonList);
        
        System.out.println("********对象 -> JSON************");
        Users us = mapper.readValue(jsonUser, Users.class);
        System.out.println(us.getMassages());
        
        List<LinkedHashMap> lis = mapper.readValue(jsonList, List.class);
        for (LinkedHashMap map : lis) {
            String json = mapper.writeValueAsString(map);
            Users uu1 = mapper.readValue(json, Users.class);
            System.out.println(uu1.getMassages());
        }
    }
}
View Code

异常处理:

JSON转实体抛出异常com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException,是由于实体中缺少JSON的某个字段引起。可通过如下配置解决:

1.通过mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)配置实现。

2.通过@JsonIgnoreProperties(ignoreUnknown = true)注解实现

 


以上是关于Jackson简单应用的主要内容,如果未能解决你的问题,请参考以下文章

Jackson的高级应用(转)

# Jackson 学习使用

在android中显示隐藏片段

Jackson 在 Spring Boot Rest 应用程序中将日期更改为一天。

springboot 底层 JackSon 的使用

Android - 如何录制简单的声音片段?