Gson to convert Java Object to/from Json

The tutorial guides you how to convert Java Object to Json and vice-versa by Gson.

Related Articles:
Jackson to convert Java Object to/from Json String
How to convert Java Object into/from XML – with Castor & Spring Boot

A. Concepts

2 important concepts need introduce: json & gson

I. Json

– JSON: JavaScript Object Notation.
– A lightweight data-interchange format.
– It is easy for machines to parse and generate.
– JSON is a text format that is completely language independent
Example:


{"id":1,"firstname":"John","lastname":"Kennedy"}

II. Gson library

Gson is a Java library for converting Java Objects into their JSON. Provide 2 methods: toJson() and fromJson() methods to convert Java objects to JSON and vice-versa.

B. Practice

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

II. Overview

1. Project Structure

structure of gson project

2. Step to do

– Create Maven project
– Add Gson dependency
– Create Java Model class
– Use Gson functions for converting
– Run and check result

III. Practice

1. Create Maven project

Open Spring Tool Suite, on main menu, choose File->New->Maven Project, check Create a simple project, Press Next, add project info then press Finish, a Maven project is created.

2. Add Gson dependency

Open pom.xml file, add Gson dependency:

<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>com.javasampleapproach</groupId>
	<artifactId>json</artifactId>
	<version>0.0.1</version>
	<name>GSON</name>
	<description>Using GSON Library for working with Json</description>
	<!-- http://mvnrepository.com/artifact/com.google.code.gson/gson -->

	<dependencies>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.0</version>
		</dependency>
	</dependencies>

</project>

3. Create Java Model class

Create a Employee model class:


package com.javasampleapproach.gson.model;

public class Employee {
	private long id;
	private String firstname;
	private String lastname;
	
	public Employee(long id, String firstname, String lastname){
		this.id = id;
		this.firstname = firstname;
		this.lastname = lastname;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	
	@Override
	public String toString() {
		String employeeInfo = String.format("Employee Info: id=%d, firstName = %s, lastname = %s", id, firstname, lastname);
		return employeeInfo;
	}
}

4. Use Gson functions for converting

– Convert Java Object to Json by toJson funtion of GSON:

 
Employee employee = new Employee(1, "John", "Kennedy");
Gson gson = new Gson();
String jsonString = gson.toJson(employee);

– Convert Json to Java Object:


Employee convertEmployee = gson.fromJson(jsonString, Employee.class);
System.out.println(convertEmployee.toString());

5. Run & Check result

– Main class:


package com.javasampleapproach.gson;

import com.google.gson.Gson;
import com.javasampleapproach.gson.model.Employee;

public class Application {
	public static void main(String[] args) {
		// 
		// Convert Java Object to JSON 
		// 
		Employee employee = new Employee(1, "John", "Kennedy");
		Gson gson = new Gson();
		String jsonString = gson.toJson(employee);
		System.out.println(jsonString);
		
		// 
		// Convert jsonString to JavaObject
		//
		Employee convertEmployee = gson.fromJson(jsonString, Employee.class);
		System.out.println(convertEmployee.toString());
		
	}
	
}

– Run with result:


{"id":1,"firstname":"John","lastname":"Kennedy"}
Employee Info: id=1, firstName = John, lastname = Kennedy

III. Source code

gson-json



By grokonez | December 8, 2016.

Last updated on April 25, 2021.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*