JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

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 OOPs?
Object oriented programming organizes a program around its data, i.e. , objects and a set of well defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

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.