FastJson 代码示例

FastJson 代码示例

示例代码中包含 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>
        <fastjson.version>2.0.41fastjson.version>
        <hutool.version>5.8.22hutool.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.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>${fastjson.version}version>
        dependency>
    dependencies>
project>


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

准备javabean

package com.lihaozhe.json.fastjson;

import com.alibaba.fastjson2.annotation.JSONField;
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;

    /**
     * 入职时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date hiredate;
    /**
     * 离职日期
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date turnoverDate;
    /**
     * 账号注册日期
     */
    @JSONField(format = "yyyy-MM-dd")
    private LocalDate createDate;
    /**
     * 账号注册时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    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 = DateUtils.toLocalDate(this.hiredate);
        this.createDateTime = DateUtils.toLocalDateTime(this.hiredate);
    }
}

javabean转json格式字符串

Emp emp = new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1);
System.out.println(emp);
String jsonString = JSONObject.toJSONString(emp);
System.out.println(jsonString);
        

结果如下:

Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:43:23 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:43:23.364)
{"account":"root","createDate":"2023-10-19","createDateTime":"2023-10-19 19:43:23","gender":1,"hiredate":"2023-10-19 19:43:23","name":"李昊哲","password":"e10adc3949ba59abbe56e057f20f883e"}

json格式字符串转javabean

Emp emp = new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1);
System.out.println(emp);
String jsonString = JSONObject.toJSONString(emp);
System.out.println(jsonString);
Emp newEmp = JSONObject.parseObject(jsonString, Emp.class);
System.out.println(newEmp);

结果如下:

Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:43:23 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:43:23.364)
{"account":"root","createDate":"2023-10-19","createDateTime":"2023-10-19 19:43:23","gender":1,"hiredate":"2023-10-19 19:43:23","name":"李昊哲","password":"e10adc3949ba59abbe56e057f20f883e"}
Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:43:23 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:43:23.364)

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);
String jsonString = JSONObject.toJSONString(empList);
System.out.println(jsonString);

结果如下:

Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:47:25 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:47:25.530)
Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:47:25 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:47:25.531)
[{"account":"root","createDate":"2023-10-19","createDateTime":"2023-10-19 19:47:25","gender":1,"hiredate":"2023-10-19 19:47:25","name":"李昊哲","password":"e10adc3949ba59abbe56e057f20f883e"},{"account":"admin","createDate":"2023-10-19","createDateTime":"2023-10-19 19:47:25","gender":0,"hiredate":"2023-10-19 19:47:25","name":"李哲","password":"e10adc3949ba59abbe56e057f20f883e"}]

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);
String jsonString = JSONObject.toJSONString(empList);
System.out.println(jsonString);
// 第二个参数是集合泛型
List<Emp> emps = JSON.parseArray(jsonString, Emp.class);
emps.forEach(System.out::println);

结果如下:

Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:47:25 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:47:25.530)
Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:47:25 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:47:25.531)
[{"account":"root","createDate":"2023-10-19","createDateTime":"2023-10-19 19:47:25","gender":1,"hiredate":"2023-10-19 19:47:25","name":"李昊哲","password":"e10adc3949ba59abbe56e057f20f883e"},{"account":"admin","createDate":"2023-10-19","createDateTime":"2023-10-19 19:47:25","gender":0,"hiredate":"2023-10-19 19:47:25","name":"李哲","password":"e10adc3949ba59abbe56e057f20f883e"}]
Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:47:25 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:47:25)
Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:47:25 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:47:25)

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));
String jsonString = JSONObject.toJSONString(map);
System.out.println(jsonString);

结果如下:

key >>> root	value >>> Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:49:18 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:49:18.467)
key >>> admin	value >>> Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:49:18 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:49:18.468)
{"root":{"account":"root","createDate":"2023-10-19","createDateTime":"2023-10-19 19:49:18","gender":1,"hiredate":"2023-10-19 19:49:18","name":"李昊哲","password":"e10adc3949ba59abbe56e057f20f883e"},"admin":{"account":"admin","createDate":"2023-10-19","createDateTime":"2023-10-19 19:49:18","gender":0,"hiredate":"2023-10-19 19:49:18","name":"李哲","password":"e10adc3949ba59abbe56e057f20f883e"}}

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));
String jsonString = JSONObject.toJSONString(map);
System.out.println(jsonString);
Map empMap = JSON.parseObject(jsonString, Map.class);
empMap.forEach((key, value) -> System.out.println("key >>> " + key + "\tvalue >>> " + value));

结果如下:

key >>> root	value >>> Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:49:18 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:49:18.467)
key >>> admin	value >>> Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:49:18 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:49:18.468)
{"root":{"account":"root","createDate":"2023-10-19","createDateTime":"2023-10-19 19:49:18","gender":1,"hiredate":"2023-10-19 19:49:18","name":"李昊哲","password":"e10adc3949ba59abbe56e057f20f883e"},"admin":{"account":"admin","createDate":"2023-10-19","createDateTime":"2023-10-19 19:49:18","gender":0,"hiredate":"2023-10-19 19:49:18","name":"李哲","password":"e10adc3949ba59abbe56e057f20f883e"}}
key >>> root	value >>> Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:49:18 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:49:18.467)
key >>> admin	value >>> Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:49:18 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:49:18.468)

你可能感兴趣的:(Java,大数据,数据分析,java,json)