JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

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


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

As a first step add google gson dependent jar file to your classpath. Here is the pom.xml file for your reference:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    					http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>gson_exmp</groupId>
  <artifactId>gson_exmp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
  		<groupId>com.google.code.gson</groupId>
  		<artifactId>gson</artifactId>
  		<version>2.3.1</version>
  	</dependency>
  </dependencies>
</project>

Create a simple Employee pojo. We will convert this pojo to JSON value.

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;
	}
}

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

package com.java2novice.json.gson;

import com.google.gson.Gson;

public class ObjectToJsonEx {

	public static void main(String a[]){
		
		Employee emp = new Employee();
		emp.setEmpId(1016);
		emp.setName("Nataraj G");
		emp.setSalary(20000);
		emp.setDesignation("Manager");
		emp.setDepartment("Accounts");
		
		Gson gsonObj = new Gson();
		// converts object to json string
		String jsonStr = gsonObj.toJson(emp);
		System.out.println(jsonStr);
	}
}

Output:
{"empId":1016,"name":"Nataraj G","designation":"Manager","department":"Accounts","salary":20000}
<< 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
true and false are keywords?
true, false, and null might seem like keywords, but they are actually literals. You cannot use them as identifiers in your programs.
Famous Quotations
We know what we are, but know not what we may be.
-- William Shakespeare

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.