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
Purpose of garbage collection
The garbage collection process is to identify the objects which are no longer referenced or needed by a program so that their resources can be reclaimed and reused. These identified objects will be discarded.
Famous Quotations
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles

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 © 2022 by Nataraja Gootooru. All Rights Reserved.