In this article, we will create an example to convert JSON representation to Java Object using the GSON library.
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. In this example, we will show you how you can serialize Java object to JSON using GSON.
Check out Convert Java Object to JSON using GSON.
Gson Maven Dependency
To use Gson with Maven2/3, you can use the Gson version available in Maven Central by adding the following dependency:
<dependencies>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
Student POJO Class - Object to be Deserialised
Let's create Student class and we will deserialize JSON representation to this Student Java object using GSON.
class Student {
private long studentId;
private String studentName;
private Set < Phone > studentPhoneNumbers = new HashSet < Phone > (0);
public long getStudentId() {
return studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Set < Phone > getStudentPhoneNumbers() {
return studentPhoneNumbers;
}
public void setStudentPhoneNumbers(Set < Phone > studentPhoneNumbers) {
this.studentPhoneNumbers = studentPhoneNumbers;
}
@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName=" + studentName + ", studentPhoneNumbers=" +
studentPhoneNumbers + "]";
}
}
class Phone {
private long phoneId;
private String phoneType;
private String phoneNumber;
public Phone() {}
public Phone(String phoneType, String phoneNumber) {
this.phoneType = phoneType;
this.phoneNumber = phoneNumber;
}
public long getPhoneId() {
return phoneId;
}
public void setPhoneId(long phoneId) {
this.phoneId = phoneId;
}
public String getPhoneType() {
return phoneType;
}
public void setPhoneType(String phoneType) {
this.phoneType = phoneType;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Deserialize or Convert JSON to Java Object using GSON
package net.javaguides.gson;
import java.util.HashSet;
import java.util.Set;
import com.google.gson.Gson;
/**
*
* @author Ramesh Fadatare
*
*/
public class GSONComplexObjectExample {
public static void main(String[] args) {
deserializeUserObject();
}
private static void deserializeUserObject() {
Gson gson = new Gson();
String jsonStr = "{\r\n" +
" \"studentId\": 1000,\r\n" +
" \"studentName\": \"Ramesh\",\r\n" +
" \"studentPhoneNumbers\": [\r\n" +
" {\r\n" +
" \"phoneId\": 100,\r\n" +
" \"phoneType\": \"Mobile Phone\",\r\n" +
" \"phoneNumber\": \"1234567890\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"phoneId\": 101,\r\n" +
" \"phoneType\": \"Landline Phone\",\r\n" +
" \"phoneNumber\": \"2222 3333 44\"\r\n" +
" }\r\n" +
" ]\r\n" +
"}";
Student student = gson.fromJson(jsonStr, Student.class);
System.out.println(student.toString());
}
}
Output:
Student [studentId=1000, studentName=Ramesh, studentPhoneNumbers=[net.javaguides.gson.Phone@5ce65a89, net.javaguides.gson.Phone@25f38edc]]
Complete Example for Reference
package net.javaguides.gson;
import java.util.HashSet;
import java.util.Set;
import com.google.gson.Gson;
/**
*
* @author Ramesh Fadatare
*
*/
public class GSONComplexObjectExample {
public static void main(String[] args) {
deserializeUserObject();
}
private static void deserializeUserObject() {
Gson gson = new Gson();
String jsonStr = "{\r\n" +
" \"studentId\": 1000,\r\n" +
" \"studentName\": \"Ramesh\",\r\n" +
" \"studentPhoneNumbers\": [\r\n" +
" {\r\n" +
" \"phoneId\": 100,\r\n" +
" \"phoneType\": \"Mobile Phone\",\r\n" +
" \"phoneNumber\": \"1234567890\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"phoneId\": 101,\r\n" +
" \"phoneType\": \"Landline Phone\",\r\n" +
" \"phoneNumber\": \"2222 3333 44\"\r\n" +
" }\r\n" +
" ]\r\n" +
"}";
Student student = gson.fromJson(jsonStr, Student.class);
System.out.println(student.toString());
}
}
class Student {
private long studentId;
private String studentName;
private Set < Phone > studentPhoneNumbers = new HashSet < Phone > (0);
public long getStudentId() {
return studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Set < Phone > getStudentPhoneNumbers() {
return studentPhoneNumbers;
}
public void setStudentPhoneNumbers(Set < Phone > studentPhoneNumbers) {
this.studentPhoneNumbers = studentPhoneNumbers;
}
@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName=" + studentName + ", studentPhoneNumbers=" +
studentPhoneNumbers + "]";
}
}
class Phone {
private long phoneId;
private String phoneType;
private String phoneNumber;
public Phone() {}
public Phone(String phoneType, String phoneNumber) {
this.phoneType = phoneType;
this.phoneNumber = phoneNumber;
}
public long getPhoneId() {
return phoneId;
}
public void setPhoneId(long phoneId) {
this.phoneId = phoneId;
}
public String getPhoneType() {
return phoneType;
}
public void setPhoneType(String phoneType) {
this.phoneType = phoneType;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Output:
Student [studentId=1000, studentName=Ramesh, studentPhoneNumbers=[net.javaguides.gson.Phone@5ce65a89, net.javaguides.gson.Phone@25f38edc]]
Comments
Post a Comment
Leave Comment