JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to convert json string to java object using Gson APIs?


This page shows how to convert json string to java object using Google gson API.

Note: Refer How to convert java object to json string using Gson APIs? page for dependent libraries.

Create a simple Employee pojo. We will map json string to this pojo.

package com.java2novice.json.gson;

public class Employee {

	private int empId;
	private String name;
	private String designation;
	private String department;
	private int salary;
	
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDesignation() {
		return designation;
	}
	public void setDesignation(String designation) {
		this.designation = designation;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
}


Input Json String:
{
    "empId": 1017,
    "name": "Nagesh Y",
    "designation": "Manager",
    "department": "Java2Novice",
    "salary": 30000
}

Finally here is the example to convert json string to java object:

package com.java2novice.json.gson;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

import com.google.gson.Gson;

public class JsonToObjectEx {

	public static void main(String a[]){
		
		BufferedReader br = null;
		Gson gsonObj = new Gson();
		try {
			br = new BufferedReader(new FileReader("/Users/ngootooru/jsonInput.txt"));
			// convert json string to object
			Employee emp = gsonObj.fromJson(br, Employee.class);
			System.out.println("Emp Name: "+emp.getName());
			System.out.println("Emp Id: "+emp.getEmpId());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output:
Emp Name: Nagesh Y
Emp Id: 1017
<< Previous Program | Next Program >>

Google gson examples

  1. How to convert java object to json string using Gson APIs?
  2. How to convert json string to java object using Gson APIs?
  3. How to convert map to json string using Gson APIs?
  4. How to enable pretty printing in Google gson APIs?
  5. How to rename JSON properties (@SerializedName) using Gson APIs?
  6. How to exclude json fields in Gson APIs?
Knowledge Centre
What is race condition?
A race condition is a situation in which two or more threads or processes are reading or writing some shared data, and the final result depends on the timing of how the threads are scheduled. Race conditions can lead to unpredictable results and subtle program bugs. A thread can prevent this from happening by locking an object. When an object is locked by one thread and another thread tries to call a synchronized method on the same object, the second thread will block until the object is unlocked.
Famous Quotations
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.