How to Convert JSON to Java object
- Convert JSON to a Java Object using Gson library in Java
- Convert JSON to Java Object using Jackson library in Java
This tutorial introduces how to convert JSON to a Java object in Java.
To convert JSON to a Java object, we can use several libraries such as Jackson and Gson. The Jackson is a Java-based JSON handling library created by Apache. Similarly, Gson is used for handling JSON and created by Google. We can use both the library to convert JSON to a Java object and vice versa.
So, let’s get started and see some working examples.
Convert JSON to a Java Object using Gson library in Java
In this example, we used the Gson library and its Gson class that reads JSON data and converts it for the specified type(Student). After conversion, we can get data by using getters of the Student class. See the example below.
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;
}
}
Output:
Rohan
Singh
California
Convert JSON to Java Object using Jackson library in Java
We used the Jackson library and its ObjectMapper
class that reads JSON data and converts for the specified type(Student). After conversion, we can get data by using getters of the Student class.
See the example below.
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;
}
}
Output:
First Name: Rohan
Last Name: Singh
City: California
Address: 34/Art
Contact: 85652321
Date of Birth: 1990-12-15
Related Article - Java JSON
- How to Pretty-Print JSON Data in Java
- How to Serialize Object to JSON in Java
- How to Convert JSON Data to String in Java
- How to Deserialize JSON in Java
- How to Handle JSON Arrays in Java