如何將Java對象轉(zhuǎn)換為JSON實(shí)例詳解
要將 Java 對象或 POJO (普通舊 Java 對象)轉(zhuǎn)換為 JSON,我們可以使用JSONObject
將對象作為參數(shù)的構(gòu)造函數(shù)之一。在下面的示例中,我們將Student
POJO 轉(zhuǎn)換為 JSON 字符串。Student
類必須提供 getter 方法,JSONObject
通過調(diào)用這些方法創(chuàng)建 JSON 字符串。
在此代碼段中,我們執(zhí)行以下操作:
- 使用 setter 方法創(chuàng)建
Student
對象并設(shè)置其屬性。 - 創(chuàng)建
JSONObject
調(diào)用object
并將Student
對象用作其構(gòu)造函數(shù)的參數(shù)。 JSONObject
使用 getter 方法生成 JSON 字符串。- 調(diào)用
object.toString()
方法獲取 JSON 字符串。
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import org.json.JSONObject; import java.util.Arrays; public class PojoToJSON { public static void main(String[] args) throws JsonProcessingException { Student student = new Student(); student.setId(1L); student.setName("Alice"); student.setAge(20); student.setCourses(Arrays.asList("Engineering", "Finance", "Chemistry")); JSONObject object = new JSONObject(student); String json = object.toString(); System.out.println(json); System.out.println(new Gson().toJson(student)); // Creating Object of ObjectMapper define in Jackson API ObjectMapper Obj = new ObjectMapper(); // Converting the Java object into a JSON string String jsonStr = Obj.writeValueAsString(student); // Displaying Java object into a JSON string System.out.println(jsonStr); } }
運(yùn)行此代碼會產(chǎn)生以下結(jié)果:
{"courses":["Engineering","Finance","Chemistry"],"name":"Alice","id":1,"age":20}
{"id":1,"name":"Alice","age":20,"courses":["Engineering","Finance","Chemistry"]}
{"id":1,"name":"Alice","age":20,"courses":["Engineering","Finance","Chemistry"]}
上面代碼中使用的Student類:
import java.util.List; public class Student { private Long id; private String name; private int age; private List<String> courses; public Student(Long id, String name, int age, List<String> courses) { this.id = id; this.name = name; this.age = age; this.courses = courses; } Student() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getCourses() { return courses; } public void setCourses(List<String> courses) { this.courses = courses; } }
Maven 依賴項(xiàng)
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.javaobjectjson</groupId> <artifactId>JavaObjectJSON</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20211205</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.1</version> </dependency> </dependencies> </project>
總結(jié)
到此這篇關(guān)于如何將Java對象轉(zhuǎn)換為JSON的文章就介紹到這了,更多相關(guān)Java對象轉(zhuǎn)換JSON內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot下swagger-ui.html訪問不到的解決方案
這篇文章主要介紹了Springboot下swagger-ui.html訪問不到的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10簡單了解Mybatis如何實(shí)現(xiàn)SQL防注入
這篇文章主要介紹了簡單了解Mybatis如何實(shí)現(xiàn)SQL防注入,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01SpringBoot核心@SpringBootApplication使用介紹
這篇文章主要介紹了SpringBoot核心@SpringBootApplication的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03如何解決@Valid對象嵌套List對象校驗(yàn)無效問題
這篇文章主要介紹了如何解決@Valid對象嵌套List對象校驗(yàn)無效問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Javafx簡單實(shí)現(xiàn)【我的電腦資源管理器】效果
這篇文章主要介紹了Javafx簡單實(shí)現(xiàn)【我的電腦資源管理器】效果,涉及Javafx操作系統(tǒng)文件模擬資源管理器的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09