fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。 fastjson是json的序列化和反序列化
一、添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
完整的pom文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>FastJsonDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>FastJsonDemo</name>
<description>FastJsonDemo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.15</version>
</plugin>
</plugins>
</build>
</project>
二、创建实体
package com.example.fastjsondemo.model;
import lombok.Data;
/**
* @author qx
* @date 2023/8/29
* @des
*/
@Data
public class Student {
private Long id;
private String name;
private Integer age;
}
package com.example.fastjsondemo.model;
import lombok.Data;
/**
* @author qx
* @date 2023/8/29
* @des
*/
@Data
public class School {
private String name;
private String address;
}
三、测试
将对象生成json
/**
* 将一个对象转成Json
*/
@Test
void testToJson() {
Student student = new Student();
student.setId(1L);
student.setName("qx");
student.setAge(20);
String json = JSONObject.toJSONString(student);
// 输出{"age":20,"id":1,"name":"qx"}
System.out.println(json);
}
json转对象
/**
* json转对象
*/
@Test
void testToObject() {
String data = "{\"age\":20,\"id\":1,\"name\":\"qx\"}";
Student student = JSONObject.parseObject(data, Student.class);
// 输出Student(id=1, name=qx, age=20)
System.out.println(student);
}
根据键获取Json数据中的值
/**
* 获取json中的值
*/
@Test
void testAccessValue() {
String data = "{\"age\":20,\"id\":1,\"name\":\"qx\"}";
JSONObject jsonObject = JSONObject.parseObject(data);
String name = jsonObject.getString("name");
// 输出qx
System.out.println(name);
int age = jsonObject.getIntValue("age");
// 输出20
System.out.println(age);
}
四、实体类中存在其他对象的情况
package com.example.fastjsondemo.model;
import lombok.Data;
/**
* @author qx
* @date 2023/8/29
* @des
*/
@Data
public class Student {
private Long id;
private String name;
private Integer age;
private School school;
}
测试
对象转Json
@Test
void testToJson() {
Student student = new Student();
student.setId(1L);
student.setName("qx");
student.setAge(20);
School school = new School();
school.setName("广西大学");
school.setAddress("南宁西乡塘区");
student.setSchool(school);
String json = JSONObject.toJSONString(student);
// 输出{"age":20,"id":1,"name":"qx","school":{"address":"南宁西乡塘区","name":"广西大学"}}
System.out.println(json);
}
Json转对象
/**
* json转对象
*/
@Test
void testToObject() {
String data = "{\"age\":20,\"id\":1,\"name\":\"qx\",\"school\":{\"address\":\"南宁西乡塘区\",\"name\":\"广西大学\"}}";
Student student = JSONObject.parseObject(data, Student.class);
// 输出Student(id=1, name=qx, age=20, school=School(name=广西大学, address=南宁西乡塘区))
System.out.println(student);
}
获取Json中的某个值
/**
* 获取json中的值
*/
@Test
void testAccessValue() {
String data = "{\"age\":20,\"id\":1,\"name\":\"qx\",\"school\":{\"address\":\"南宁西乡塘区\",\"name\":\"广西大学\"}}";
JSONObject jsonObject = JSONObject.parseObject(data);
JSONObject school = jsonObject.getJSONObject("school");
String name = school.getString("name");
// 输出 广西大学
System.out.println(name);
}
本文转载自: https://blog.csdn.net/qinxun2008081/article/details/132560802
版权归原作者 qinxun2008081 所有, 如有侵权,请联系我们删除。
版权归原作者 qinxun2008081 所有, 如有侵权,请联系我们删除。