JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

How to rename JSON properties (@SerializedName) using Gson APIs?


This page shows how to rename json properties when using Gson APIs. @SerializedName annotation helps you to modify the json property names. Annotate POJO members with @SerializedName annotation with desired property name.

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

Here is the sample POJO:

package com.java2novice.json.gson;

import com.google.gson.annotations.SerializedName;

public class Employee {

	@SerializedName("emp_id")
	private int empId;
	
	@SerializedName("emp_name")
	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;
	}
}

package com.java2novice.json.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

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 GsonBuilder().setPrettyPrinting().create();
		// converts object to json string
		String jsonStr = gsonObj.toJson(emp);
		System.out.println(jsonStr);
	}
}

Output:
{
  "emp_id": 1016,
  "emp_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
Interface and its usage
Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for declaring methods that one or more classes are expected to implement, capturing similarities between unrelated classes without forcing a class relationship and determining an object's programming interface without revealing the actual body of the class.
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 © 2020 by Nataraja Gootooru. All Rights Reserved.