springboot項(xiàng)目中使用JOSN解析庫(kù)的方法
一、JSON解析簡(jiǎn)介
在當(dāng)下流行的前后端分離的項(xiàng)目中,傳遞數(shù)據(jù)時(shí)不可或缺的。為了保證在傳遞數(shù)據(jù)的過(guò)程中不丟失信息,就需要一種讓前端和后端都識(shí)別的傳遞數(shù)據(jù)的格式,這種傳遞數(shù)據(jù)的格式就是JSON。其中,前端需要的是以“鍵:值”結(jié)構(gòu)保存的JSON數(shù)據(jù),后端需要的是JavaBean。
JSON,全程是JavaScript Object Notation,是一種輕量級(jí)的數(shù)據(jù)交換格式。所謂數(shù)據(jù)交換格式,指的是前端和后端之間傳遞數(shù)據(jù)的格式。
相比于XML格式,JSON是輕量級(jí)的。
JSON格式例子:
{“name”:“sun”}
{
“name”:“sun”,
“age”:22
}
{“arr”:[1,2,3,4]}
{“people”:{“id”:111,“name”:“sun”,“age”:22}}對(duì)于一個(gè)前后端分離的Spring Boot項(xiàng)目而言,前端需要的是以“鍵:值”結(jié)構(gòu)保存的JSON數(shù)據(jù),后端需要的是JavaBean,這就需要使用json解析庫(kù)實(shí)現(xiàn)序列化與反序列化。
序列化指的是JavaBean轉(zhuǎn)化為JSON數(shù)據(jù),反序列化反之。
當(dāng)前常用的兩種JSON解析庫(kù),一種是Spring Boot內(nèi)置的Jackson,另一種是由阿里巴巴開(kāi)發(fā)的FastJson。
下面程序?qū)嵗褂肍astJson舉例。
二、Spring Boot項(xiàng)目中使用JSON解析
1、pom.xml文件引入依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>整體pom.xml文件內(nèi)容
<?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>3.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mr</groupId>
<artifactId>_20250603spring_fastjson</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>20250603spring_fastjson</name>
<description>20250603spring_fastjson</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</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.9</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>
</plugin>
</plugins>
</build>
</project>2、編寫(xiě)Controller
序列化方法
String text = JSON.toJSONString(obj)
反序列化方法
VO vo = JSON.parseObject(json, VO.class)
注:
obj:被轉(zhuǎn)換的對(duì)象
VO:與JSON數(shù)據(jù)對(duì)應(yīng)的實(shí)體類
package com.mr._20250603spring_fastjson;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class Controller {
Bean bean = new Bean("sun",22);
// @GetMapping
@RequestMapping("/login")
public String login(@RequestBody String json){
Map loginDate = JSON.parseObject(json, Map.class);
String username = loginDate.get("username").toString();
String password = loginDate.get("password").toString();
Map<String,String> result = new HashMap<>();
String code = "";
String msg = "";
if ("mr".equals(username) && "123".equals(password)){
code = "200";
msg = "登錄成功";
}else {
code = "500";
msg = "賬號(hào)或密碼錯(cuò)誤";
}
result.put("code",code);
result.put("msg",msg);
return JSON.toJSONString(result);
// System.out.println("wdwdwdw");
// System.out.println("bean"+bean);
// String str = JSON.toJSONString(bean);
// System.out.println("bean_json"+ str);
// return str;
}
}3、apipost工具測(cè)試
3.1請(qǐng)求格式為json,請(qǐng)求參數(shù)值與程序邏輯判斷值一致時(shí)

3.2請(qǐng)求格式為json,請(qǐng)求參數(shù)值與程序邏輯判斷值不一致時(shí)

三、遇到的問(wèn)題
請(qǐng)求時(shí)注意根據(jù)驗(yàn)證的請(qǐng)求體格式選擇

到此這篇關(guān)于springboot JOSN解析庫(kù)的文章就介紹到這了,更多相關(guān)springboot json解析庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Security @PreAuthorize注解分析
本教程介紹了如何使用 Spring 方法級(jí)安全和 @PreAuthorize 注解來(lái)保護(hù) RestController 方法,通過(guò)這些步驟,您可以確保只有具有適當(dāng)角色或權(quán)限的用戶才能訪問(wèn)特定的 REST API,感興趣的朋友跟隨小編一起看看吧2024-11-11
深入淺析Java中普通代碼塊、構(gòu)造代碼塊與靜態(tài)代碼塊
這篇文章主要介紹了Java中普通代碼塊、構(gòu)造代碼塊與靜態(tài)代碼塊的相關(guān)資料,靜態(tài)代碼塊>Main()>構(gòu)造代碼塊 。非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
Springboot注解@Value讀取配置文件參數(shù)詳解
Spring Boot提供了靈活的配置文件讀取機(jī)制,主要有兩種方式,第一種是使用@Value注解直接在類屬性上讀取application.yml文件中的配置,這種方式簡(jiǎn)單直接,但需要為每個(gè)配置項(xiàng)單獨(dú)設(shè)置屬性,第二種方式是通過(guò)@PropertySource注解讀取自定義的Properties文件2024-11-11
EasyExcel工具讀取Excel空數(shù)據(jù)行問(wèn)題的解決辦法
EasyExcel是阿里巴巴開(kāi)源的一個(gè)excel處理框架,以使用簡(jiǎn)單,節(jié)省內(nèi)存著稱,下面這篇文章主要給大家介紹了關(guān)于EasyExcel工具讀取Excel空數(shù)據(jù)行問(wèn)題的解決辦法,需要的朋友可以參考下2022-08-08
模仿mybatis-plus實(shí)現(xiàn)rpc調(diào)用
這篇文章主要為大家介紹了模仿mybatis-plus實(shí)現(xiàn)rpc調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Java中@DateTimeFormat注解與@JsonFormat注解的使用方式
這篇文章主要介紹了Java中@DateTimeFormat注解與@JsonFormat注解的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot使用Mybatis-Generator配置過(guò)程詳解
這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Java中的自動(dòng)裝箱與自動(dòng)拆箱的實(shí)現(xiàn)
自動(dòng)裝箱和自動(dòng)拆箱使得我們?cè)谑褂没緮?shù)據(jù)類型時(shí)更加方便,同時(shí)也提高了代碼的可讀性和健壯性,本文將詳細(xì)介紹Java中的自動(dòng)裝箱和自動(dòng)拆箱機(jī)制,感興趣的可以了解一下2023-08-08

