• Jackson 代码示例


    Jackson代码示例

    示例代码中包含 Date LocalDate LocalDateTime 类型处理方式
    JavaBean 与 json 相互转换 bean2json json2bean
    List 与 json 相互转换 list2json json2list
    Map 与 json 相互转换map2json json2map

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <groupId>com.lihaohzegroupId>
        <artifactId>chap06artifactId>
        <version>1.0.0version>
        <packaging>jarpackaging>
        <name>chap06name>
        <url>https://mvnrepository.com/url>
    
        <properties>
            
            <maven.compiler.source>21maven.compiler.source>
            <maven.compiler.target>21maven.compiler.target>
            <maven.compiler.compilerVersion>21maven.compiler.compilerVersion>
            <maven.compiler.encoding>utf-8maven.compiler.encoding>
            <project.build.sourceEncoding>utf-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
            <maven.test.failure.ignore>truemaven.test.failure.ignore>
            <maven.test.skip>truemaven.test.skip>
    
            <commons-io.version>2.14.0commons-io.version>
            <commons-lang3.version>3.13.0commons-lang3.version>
            <hutool.version>5.8.22hutool.version>
            <jackson.version>2.15.3jackson.version>
            <junit.version>5.10.0junit.version>
            <lombok.version>1.18.30lombok.version>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>org.junit.jupitergroupId>
                <artifactId>junit-jupiter-apiartifactId>
                <version>${junit.version}version>
                
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>org.junit.jupitergroupId>
                <artifactId>junit-jupiter-engineartifactId>
                <version>${junit.version}version>
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
                <scope>providedscope>
            dependency>
            
            <dependency>
                <groupId>cn.hutoolgroupId>
                <artifactId>hutool-allartifactId>
                <version>${hutool.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-lang3artifactId>
                <version>${commons-lang3.version}version>
            dependency>
            
            <dependency>
                <groupId>commons-iogroupId>
                <artifactId>commons-ioartifactId>
                <version>${commons-io.version}version>
            dependency>
            
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-coreartifactId>
                <version>${jackson.version}version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-annotationsartifactId>
                <version>${jackson.version}version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>${jackson.version}version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.datatypegroupId>
                <artifactId>jackson-datatype-jsr310artifactId>
                <version>${jackson.version}version>
            dependency>
        dependencies>
    project>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

    javabean与json格式字符串相互转换

    准备javabean

    package com.lihaozhe.json.jackson;
    
    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
    import com.fasterxml.jackson.databind.annotation.JsonSerialize;
    import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
    import com.fasterxml.jackson.databind.ser.std.DateSerializer;
    import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
    import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
    import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
    import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
    import com.lihaozhe.util.date.DateUtils;
    import lombok.*;
    
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.util.Date;
    
    /**
     * @author 李昊哲
     * @version 1.0
     * @create 2023/10/19
     */
    @Getter
    @Setter
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public class Emp {
        /**
         * 账号
         */
        private String account;
        /**
         * 密码
         */
        private String password;
        /**
         * 姓名
         */
        private String name;
        /**
         * 性别 1 代表男性 0 代表女性
         */
        private int gender;
    
        /**
         * 入职时间
         */
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH-mm-ss", locale = "zh", timezone = "GMT+8")
        @JsonSerialize(using = DateSerializer.class)
        @JsonDeserialize(using = DateDeserializers.DateDeserializer.class)
        private Date hiredate;
        /**
         * 离职日期
         */
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH-mm-ss", locale = "zh", timezone = "GMT+8")
        @JsonSerialize(using = DateSerializer.class)
        @JsonDeserialize(using = DateDeserializers.DateDeserializer.class)
        private Date turnoverDate;
        /**
         * 日期
         */
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", locale = "zh", timezone = "GMT+8")
        @JsonSerialize(using = LocalDateSerializer.class)
        @JsonDeserialize(using = LocalDateDeserializer.class)
        private LocalDate createDate;
        /**
         * 账号停用时间
         */
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH-mm-ss", locale = "zh", timezone = "GMT+8")
        @JsonSerialize(using = LocalDateTimeSerializer.class)
        @JsonDeserialize(using = LocalDateTimeDeserializer.class)
        private LocalDateTime createDateTime;
    
        public Emp(String account, String password, String name, int gender) {
            this.account = account;
            this.password = password;
            this.name = name;
            this.gender = gender;
            this.hiredate = new Date();
            this.createDate = LocalDate.now();
            this.createDateTime = LocalDateTime.now();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    javabean转json格式字符串

    Emp emp = new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1);
    System.out.println(emp);
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(emp);
    System.out.println(json);
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    结果如下:

    Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:58:06 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:58:06.343)
    {"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19-58-06","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-58-06"}
    
    
    • 1
    • 2
    • 3

    json格式字符串转javabean

    Emp emp = new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1);
    System.out.println(emp);
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(emp);
    System.out.println(json);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    结果如下:

    Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:58:06 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:58:06.343)
    {"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19-58-06","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-58-06"}
    Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:58:06 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:58:06.343)
    
    
    • 1
    • 2
    • 3
    • 4

    list与json格式字符串相互转换

    list转json格式字符串

    List<Emp> empList = new ArrayList<>();
    empList.add(new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1));
    empList.add(new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李哲", 0));
    empList.forEach(System.out::println);
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(empList);
    System.out.println(json);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    结果如下:

    Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.321)
    Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.323)
    [{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19-59-09","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-59-09"},{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李哲","gender":0,"hiredate":"2023-10-19 19-59-09","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-59-09"}]
    
    
    • 1
    • 2
    • 3
    • 4

    json格式字符串转list

    List<Emp> empList = new ArrayList<>();
    empList.add(new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1));
    empList.add(new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李哲", 0));
    empList.forEach(System.out::println);
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(empList);
    System.out.println(json);
    // JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Emp.class);
    // List emps = objectMapper.readValue(json, javaType);
    List<Emp> emps = objectMapper.readValue(json, new TypeReference<List<Emp>>() {
            });
    emps.forEach(System.out::println);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    结果如下:

    Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.321)
    Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.323)
    [{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19-59-09","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-59-09"},{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李哲","gender":0,"hiredate":"2023-10-19 19-59-09","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-59-09"}]
    Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.321)
    Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.323)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    map与json格式字符串相互转换

    map转json格式字符串

    Map<String, Emp> map = new HashMap<>();
    map.put("root", new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1));
    map.put("admin", new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李哲", 0));
    map.forEach((key, value) -> System.out.println("key >>> " + key + "\tvalue >>> " + value));
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(map);
    System.out.println(json);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    结果如下:

    key >>> root	value >>> Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.568)
    key >>> admin	value >>> Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.570)
    {"root":{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 20-01-52","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 20-01-52"},"admin":{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李哲","gender":0,"hiredate":"2023-10-19 20-01-52","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 20-01-52"}}
    
    
    • 1
    • 2
    • 3
    • 4

    json格式字符串转map

    Map<String, Emp> map = new HashMap<>();
    map.put("root", new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1));
    map.put("admin", new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李哲", 0));
    map.forEach((key, value) -> System.out.println("key >>> " + key + "\tvalue >>> " + value));
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(map);
    System.out.println(json);
    // MapType mapType = objectMapper.getTypeFactory().constructMapType(Map.class, String.class, Emp.class);
    // Map empMap = objectMapper.readValue(json, mapType);
    Map<String, Emp> empMap = objectMapper.readValue(json, new TypeReference<Map<String, Emp>>() {
    });
    empMap.forEach((key, value) -> System.out.println("key >>> " + key + "\tvalue >>> " + value));
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    结果如下:

    key >>> root	value >>> Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.568)
    key >>> admin	value >>> Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.570)
    {"root":{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 20-01-52","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 20-01-52"},"admin":{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李哲","gender":0,"hiredate":"2023-10-19 20-01-52","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 20-01-52"}}
    key >>> root	value >>> Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.568)
    key >>> admin	value >>> Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.570)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    前端vue项目加快热更新——提高部署速度——采用babel-plugin-dynamic-import-node插件
    Kotlin学习之2
    Ocelot使用与设置路由Routing
    Linux通过QQ邮箱账号使用mailx发送邮件
    皮肤性病科专家谭巍主任提出HPV转阴后饮食七点建议
    【矩阵论】4. 矩阵运算——矩阵拉直
    python--使用pika库操作rabbitmq实现需求
    Arduino基础入门
    springboot常用组件的集成
    基于Spring Cloud搭建分布式配置中心
  • 原文地址:https://blog.csdn.net/qq_24330181/article/details/133933678