将 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