• FastJson的简单使用


    前言

    关于FastJson大家一定都有了解,那么对于刚接触的朋友来说可能有点半懵半懂的感觉,那么我就来分享一下自己的理解,如果有不对的地方希望大家指出来,共同进步~

    Fastjson 简介

    Fastjson 是一个 Java 库,可以将 Java 对象转换为 JSON 格式,当然它也可以将 JSON 字符串转换为 Java 对象。
    Fastjson 可以操作任何 Java 对象,即使是一些预先存在的没有源码的对象。
    Fastjson 源码地址:https://github.com/alibaba/fastjson
    Fastjson 中文 Wiki:https://github.com/alibaba/fastjson/wiki/Quick-Start-CN

    二、使用步骤

    1.引入Pom依赖

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>x.x.x</version>
    </dependency>
    其中 x.x.x 是版本号,根据需要使用特定版本,建议使用最新版本。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    实体类:
    @Data
    public class Student {
        private Integer id;
        private String name;
        private Integer age;
        private String email;
        private Date birthday;
        private String address;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    对象 与 JSON字符串 互相转换

    //对象(可以是任何你能想到的格式,简单对象,集合,对象集合等)
    Student student = new Student();
    
    // Object --> JSONString
    String jsonString = JSON.toJSONString(student);
    // JSONString --> Object
    Student p2 = JSON.parseObject(jsonString, Student.class);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    List 与 JSON字符串 互相转换

    //对象(可以是任何你能想到的格式,简单对象,集合,对象集合等)
    List<Student> list = new ArrayList<>();
    Student student1 = new Student();
    list.add(student1);
    
    // List --> JSONString
    String JSONString = JSON.toJSONString(list);
    // JSONString --> List
    List<Student> studentList = JSON.parseArray(JSONString, Student.class);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Map 与 JSON字符串 互相转换

    Map<String, Student> map = new HashMap();
    Student student1 = new Student();
    map.put("student1", student1);
    
    // Map --> JSONString
    String JSONString = JSON.toJSONString(map);
    // JSONString --> Map
    Map<String, Student> studentMap = JSON.parseObject(JSONString, new TypeReference<Map<String, Student>>(){});
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    SerializerFeature枚举

    该枚举支持序列化的一些特性数据定义

    • 枚举常量 WriteMapNullValue 序列化为null的字段(如果某字段没有设置值,序列化为null)
    • 枚举常量 WriteNullStringAsEmpty 字符串类型字段为null,序列化为空字符串""
    • 枚举常量 WriteNullNumberAsZero 数值类型字段为null,序列化为0
    • 枚举常量 WriteNullBooleanAsFalse Boolean类型字段值为null 输出false
    • 枚举常量 WriteDateUseDateFormat 日期类型字段格式化日期格式
    • 枚举常量 PrettyFormat格式化输出
    
        @Test
        public void testSerializerFeature() {
            Student student = new Student();
            student.setId(1);
            student.setName("张三");
            //student.setAge(20);
            //student.setAddress("北京市");
            student.setBirthday(new Date());
            student.setEmail("zs@sina.com");
            String jsonString1 = JSON.toJSONString(student);
            System.out.println(jsonString1);
            // {"birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
    
            String jsonString2 = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
            System.out.println(jsonString2);
            // {"address":null,"age":null,"birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
            
            String jsonString3 = JSON.toJSONString(student, SerializerFeature.WriteNullStringAsEmpty);
            System.out.println(jsonString3);
            // {"address":"","birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
            
            String jsonString4 = JSON.toJSONString(student, SerializerFeature.WriteNullNumberAsZero);
            System.out.println(jsonString4);
            // {"age":0,"birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
            
            String jsonString5 = JSON.toJSONString(student, SerializerFeature.WriteNullBooleanAsFalse);
            System.out.println(jsonString5);
            // {"birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
            
            String jsonString6 = JSON.toJSONString(student, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.PrettyFormat);
            System.out.println(jsonString6);
            //    {
            //        "birthday":"2022-06-26 11:47:15",
            //            "email":"zs@sina.com",
            //            "id":1,
            //            "name":"张三"
            //    }
        }
    
    
    • 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

    @JSonField注解

    该注解作用于方法上,字段上和参数上.可在序列化和反序列化时进行特性功能定制.

    • 注解属性 : name 序列化后的名字
    • 注解属性 : ordinal序列化后的顺序
    • 注解属性 : format 序列化后的格式
    • 注解属性 : serialize 是否序列化该字段
    • 注解属性 : deserialize 是否反序列化该字段
    • 注解属性 : serialzeFeatures 序列化时的特性定义

    @ JSonType注解

    该注解作用于类上,对该类的字段进行序列化和反序列化时的特性功能定制.

    • 注解属性 : includes 要被序列化的字段.
    • 注解属性 : orders 序列化后的顺序.
    • 注解属性 : serialzeFeatures 序列化时的特性定义.

    另附:菜鸟教程链接:https://www.runoob.com/w3cnote/fastjson-intro.html

  • 相关阅读:
    WPF + Winform 解决管理员权限下无法拖放文件的问题
    Hadoop中的MapReduce框架原理、Job提交流程源码断点在哪断并且介绍相关源码、切片与MapTask并行度决定机制、MapTask并行度决定机制
    SpringBoot整合ElasticEearch【代码示例】
    05【数组的扩展】
    [Java]Redission入门使用
    C#中的Dispatcher:Invoke与BeginInvoke的使用
    Toolformer论文阅读笔记(简略版)
    [Spring Boot]08 IDEA接入MyBatisCodeHelper代码自动生成器
    Ansible 批处理实战
    22-09-20 西安 谷粒商城(04)Redisson、布隆过滤器、AOP赋能自定义注解@GmallCache
  • 原文地址:https://blog.csdn.net/weixin_42472048/article/details/125455855