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
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Famous Quotations
Discipline is just choosing between what you want now and what you want most.
-- Unknown Author

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.