How to convert Java object to JSON and JSON to Java object with Gson

Published on March 23, 2016 by Daniel Lanza


A simple Java object will become to JSON by using Gson. Besides, this will become into Java object.

Maven dependencies


User Java class

package com.dalanz.json;

import java.util.ArrayList;
import java.util.List;

public class User {

    private String firstName;
    private String lastName;
    private int age;
    private List<String> messages;

    public User(){}

    public User(String firstName, String lastName, int age, List<String> messages) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.messages = messages;

    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;

    public int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

    public List<String> getMessages() {
        return messages;

    public void setMessages(List<String> messages) {
        this.messages = messages;

    public void userDefault(){
        this.firstName = "Daniel";
        this.lastName = "Lanza";
        this.age = 30;
        this.messages = new ArrayList<String>();
        this.messages.add("Message 1");
        this.messages.add("Message 2");
        this.messages.add("Message 3");

Gson Example class

package com.dalanz.json;


public class GsonExample {

    private String json;
    private User user;

    public String getJson() {
        return json;
    public void setJson(String json) {
        this.json = json;
    public User getUser() {
        return user;
    public void setUser(User user) {
        this.user = user;

    public GsonExample(){
        this.user = null;

    public String userToJson(){
        Gson gson = new Gson();
        return gson.toJson(this.user);

    public User jsonToUser(){
        Gson gson = new Gson();
        return gson.fromJson(this.json, User.class);

Main Class


import com.dalanz.json.*;

public class test {

    public static void main(String[] args) {

        User user1 = new User();

        GsonExample gson1 = new GsonExample();
        GsonExample gson2 = new GsonExample();
        GsonExample gson3 = new GsonExample();

        // Gson1 -> JSON from Java Object
        String jsonUser1 = gson1.userToJson();
        System.out.println("User 1 in JSON: " + jsonUser1);

        // Gson 2 -> Java Object from JSON (gson1)
        User user2 = gson2.jsonToUser();
        System.out.println("User 2 calculated from JSON User 1");

        // Gson3 -> JSON from Java Object (gson2)
        String jsonUser3 = gson1.userToJson();
        System.out.println("User 2 in JSON: " + jsonUser3);

        if (jsonUser1.equals(jsonUser3))


User 1 in JSON: {"firstName":"Daniel","lastName":"Lanza","age":30,"messages":["Message 1","Message 2","Message 3"]}
User 2 calculated from JSON User 1
User 2 in JSON: {"firstName":"Daniel","lastName":"Lanza","age":30,"messages":["Message 1","Message 2","Message 3"]}
comments powered by Disqus

See also

How to zip a folder and unzip a Zip file in Java

A folder with subfolders and files will be zipped. Besides, the zip file will be unzipped. Zip directory Java class package com.dalanz.file; import; import; import; import; import java.util.ArrayList; import; import; import; public class ZipDirectory { private String folderPath; private String zipPath; private static ArrayList<String> fileList = null; private static String sourceFolder = null; public String getFolderPath() { return folderPath; } public void setFolderPath(String folderPath) { this.

Read more

How to convert File into Array of Bytes, and Array of Bytes to File in Java

Array of bytes will be calculated from a file, and then, that array of bytes will be used to create a file with different filename. Array of bytes Java class package com.dalanz.file; import; import; import; public class ArrayBytesFile { // Attributes private File file; private byte[] bFile; // Constructors public ArrayBytesFile() { this.file = null; this.bFile = null; } public ArrayBytesFile (File file) { this.file = file; fileToBFile(); } public ArrayBytesFile (String filePath, byte[] bFile) { this.

Read more

How to get MD5 checksum for a file in Java

MD5 checksum is a 128-bit hash value (32 digit hexadecimal number). This works as a fingerprint for a file which let us compare together other files to find out duplicate files. These Java classes will let us calculate MD5 checksum for any single file. Maven dependencies <dependencies> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> MD5 Java class package com.dalanz.file; import; import; import; import org.

Read more