Deserialize JSON to Object Example

This program demonstrates deserializing JSON into a Java object.

Problem Statement

Write a Java program that deserializes JSON string into Student object using Gson library.

Source Code

1 import com.google.gson.Gson;
2 class Student {
3 int id; String name;
4 }
5 public class JSONDeserializeExample {
6 public static void main(String[] args) {
7 System.out.println("Eduinq JSON Deserialize Example");
8 Gson gson=new Gson();
9 String json="{\"id\":1,\"name\":\"Arun\"}";
10 Student s=gson.fromJson(json,Student.class);
11 System.out.println("Eduinq Student: "+s.id+" "+s.name);
12 }
13 }

Program Output

					Eduinq JSON Deserialize Example
Eduinq Student: 1 Arun
				

Explanation

Uses Gson fromJson() to convert JSON. - Restores object from JSON string. - Exception handling ensures safe operations. - In enterprise systems, JSON deserialization supports data exchange. Conclusion: Shows deserializing JSON to objects.

Quick Links to Explore