栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

FastJson

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

FastJson

1. Json 的概述

  JSON 是一种轻量级的数据格式。客户端和服务器之间传输的数据就是采用 JSON 的格式进行传输

2. FastJson 的概述

  FastJson 是阿里巴巴的开源 JSON 解析库,它可以解析 JSON 格式的字符串,支持将 Java Bean 序列化为 JSON 字符串,也可以从 JSON 字符串反序列化到 Java Bean


  FastJson 优点:

速度快使用广泛功能完备使用简单 3. 引入 FastJson 的依赖


    com.alibaba
    fastjson
    1.2.78

4. FastJson 的序列化

  序列化:是指将 Java 对象转化成 json 格式字符串的过程。JavaBean 对象、List 集合对象、Map 集合,为应用最广泛的

(1) 创建一个测试实体类
package com.peng.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String email;
    private Date birth;
}
(2) Java 对象序列化为 Json 格式字符串
package com.peng.test;

import com.alibaba.fastjson.JSON;
import com.peng.domain.Student;

import java.util.Date;

public class demo {
    public static void main(String[] args) {
        Student student = new Student(1, "张三", 20, "zs@qq.com", new Date());
        // 将 Student 对象转化为 JSON 格式的字符串
        String jsonString = JSON.toJSONString(student);
        System.out.println(jsonString);
        // {"age":20,"birth":1643528834792,"email":"zs@qq.com","id":1,"name":"张三"}
    }
}

(3) List 集合序列化为 Json 字符串
package com.peng.test;

import com.alibaba.fastjson.JSON;
import com.peng.domain.Student;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class demo {
    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add(new Student(1, "张三", 20, "zs@qq.com", new Date()));
        list.add(new Student(2, "李四", 18, "lisi@qq.com", new Date()));
        // 将 List 集合序列化为 JSON 格式的字符串
        String jsonString = JSON.toJSONString(list);
        // 转后的结果是数组,数组的元素是对象
        System.out.println(jsonString);
        // [{"age":20,"birth":1643530026118,"email":"zs@qq.com","id":1,"name":"张三"},{"age":18,"birth":1643530026118,"email":"lisi@qq.com","id":2,"name":"李四"}]
    }
}
(4) Map 集合序列化为 Json 字符串
package com.peng.test;

import com.alibaba.fastjson.JSON;
import com.peng.domain.Student;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class demo {
    public static void main(String[] args) {
        Map map = new HashMap<>();
        map.put("student1", new Student(1, "张三", 20, "zs@qq.com", new Date()));
        map.put("student2", new Student(2, "李四", 18, "lisi@qq.com", new Date()));
        String jsonString = JSON.toJSONString(map);
        // map 集合转化后的结果仍然是对象
        System.out.println(jsonString);
        // {"student2":{"age":18,"birth":1643530736537,"email":"lisi@qq.com","id":2,"name":"李四"},"student1":{"age":20,"birth":1643530736537,"email":"zs@qq.com","id":1,"name":"张三"}}
    }
}

对象和 Map 集合转化后的 json 格式还是对象,List 集合转化后的 json 格式是数组

5. FastJson 的反序列化

  反序列化:把 json 格式的字符串转化成 Java 对象

(1) json 格式字符串反序列化为 Java 对象
package com.peng.test;

import com.alibaba.fastjson.JSON;
import com.peng.domain.Student;

public class demo {
    public static void main(String[] args) {
        String jsonString = "{"age":20,"birth":1643528834792,"email":"zs@qq.com","id":1,"name":"张三"}";
        // 参数1:反序列化的 json 字符串;参数2:Java 对象的 class 对象
        Student student = JSON.parseObject(jsonString, Student.class);
        System.out.println(student);
    }
}
(2) json 字符串反序列化为 List 集合
package com.peng.test;

import com.alibaba.fastjson.JSON;
import com.peng.domain.Student;

import java.util.List;

public class demo {
    public static void main(String[] args) {
        String jsonString = "[{"age":20,"birth":1643530026118,"email":"zs@qq.com","id":1,"name":"张三"},{"age":18,"birth":1643530026118,"email":"lisi@qq.com","id":2,"name":"李四"}]";
        // 参数1:反序列化的 json 字符串;参数2:转换后集合的泛型的 class 对象
        List list = JSON.parseArray(jsonString, Student.class);
        System.out.println(list);
    }
}
(3) json 字符串反序列化为 Map 集合
package com.peng.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.peng.domain.Student;

import java.util.Map;

public class demo {
    public static void main(String[] args) {
        String jsonString = "{"student2":{"age":18,"birth":1643530736537,"email":"lisi@qq.com","id":2,"name":"李四"},"student1":{"age":20,"birth":1643530736537,"email":"zs@qq.com","id":1,"name":"张三"}}";
        // 参数1:反序列化的 json 字符串;参数2:TypeReference 类型,在泛型中写转换后的 Map 集合
        Map map = JSON.parseObject(jsonString, new TypeReference>() {});
        map.forEach((k, v) -> System.out.println(k + "::" + v));
    }
}
6. @JsonType 注解

  只能作用在实体类上,对类的字段进行序列化和反序列化时的特性功能的定制

package com.peng.domain;

import com.alibaba.fastjson.annotation.JSONType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor

@JSONType(includes = {"id", "name", "age", "birth"}, orders = {"name", "age", "id", "birth"})
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String email;
    private Date birth;
}
7. @JsonField 注解

  一般作用在实体类的属性上,可在序列化和反序列化时进行特性功能的定制

package com.peng.domain;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private Integer id;
    
    @JSONField(name = "studentName")
    private String name;
    private Integer age;
    
    @JSONField(serialize = false)
    private String email;
    
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date birth;
}
8. spring boot 与 FastJson

  spring boot默认的序列化方式是 jackson。我们如果想切换成 FastJson,需要配置一下

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/719586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号