將 JSON 轉換為 Java 物件
Mohammad Irfan
2023年10月12日
本教程介紹如何在 Java 中將 JSON 轉換為 Java 物件。
要將 JSON 轉換為 Java 物件,我們可以使用幾個庫,例如 Jackson 和 Gson。Jackson 是由 Apache 建立的基於 Java 的 JSON 處理庫。同樣,Gson 用於處理 JSON,由 Google 建立。我們可以使用這兩個庫將 JSON 轉換為 Java 物件,反之亦然。
所以,讓我們開始吧,看看一些工作示例。
使用 Java 中的 Gson 庫將 JSON 轉換為 Java 物件
在此示例中,我們使用了 Gson 庫及其 Gson 類,該類讀取 JSON 資料並將其轉換為指定型別(Student)。轉換後,我們可以使用 Student 類的 getter 來獲取資料。請參閱下面的示例。
import com.google.gson.Gson;
public class SimpleTesting {
public static void main(String[] args) throws InterruptedException {
String json = "" "
{
"firstName" : "Rohan",
"lastName" : "Singh",
"dateOfBirth" : "1990-12-15",
"address" : "34/Art",
"city" : "California",
"contact" : "85652321"
}
"" ";
Student data = new Gson().fromJson(json, Student.class);
System.out.println(data.getFirstName());
System.out.println(data.getLastName());
System.out.println(data.getCity());
}
}
class Student {
private String firstName;
private String lastName;
private String dateOfBirth;
private String address;
private String city;
private String contact;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
輸出:
Rohan
Singh
California
使用 Java 中的 Jackson 庫將 JSON 轉換為 Java 物件
我們使用 Jackson 庫及其 ObjectMapper
類讀取 JSON 資料並轉換為指定型別(Student
)。轉換後,我們可以使用 Student 類的 getter 來獲取資料。
請參閱下面的示例。
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.File;
import java.io.IOException;
public class SimpleTesting {
public static void main(String[] args) throws InterruptedException {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
try {
Student student = mapper.readValue(new File("json-file.json"), Student.class);
System.out.println("First Name: " + student.getFirstName());
System.out.println("Last Name: " + student.getLastName());
System.out.println("City: " + student.getCity());
System.out.println("Address: " + student.getAddress());
System.out.println("Contact: " + student.getContact());
System.out.println("Date of Birth: " + student.getDateOfBirth());
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Student {
private String firstName;
private String lastName;
private String dateOfBirth;
private String address;
private String city;
private String contact;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
輸出:
First Name: Rohan
Last Name: Singh
City: California
Address: 34/Art
Contact: 85652321
Date of Birth: 1990-12-15