Convert from JSON format to Java object and vice versa

In this post, Gson library is used to serialize data in JSON format to Java object and deserialize it back.
Gson is an open source java library created by Google.
Nulls are omitted from the JSON output.

– toJson() is used to serialize an object to JSON format
   gson.toJson( object );
– fromJson() is used to deserialize JSON format to an object
   gson.fromJson( jsonString, <Class> );

Maven dependency:

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

Example 1: POJO to JSON and back

package com.codebonneamie.demo;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;

private void convertToJson()
{
    Country country = new Country();
    List<Language> languages = new ArrayList<Language>();
    Language language = null;
    country.setName( “India” );
    language = new Language();
    language.setName( “Hindi” );
    language.setFamily( “Indo-European” );
    languages.add( language );
    language = new Language();
    language.setName( “Tamil” );
    language.setFamily( “Dravidian” );
    languages.add( language );
    country.setLanguages( languages );

    Gson gson = new Gson();
    String jsonString = gson.toJson( country );
    System.out.println( “jsonString: ” + jsonString );

    Country c = gson.fromJson( jsonString, Country.class );
    System.out.println( c );
 }


class Country
{
private String name;
private List<Language> languages;

public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public List<Language> getLanguages()
{
return languages;
}
public void setLanguages( List<Language> languages )
{
this.languages = languages;
}
@Override
public String toString()
{
return “Country [name=” + name + “, languages=” + languages + “]”;
}
}
class Language
{
private String name;
private String family;

public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public String getFamily()
{
return family;
}
public void setFamily( String family )
{
this.family = family;
}
@Override
public String toString()
{
return “Language [name=” + name + “, family=” + family + “]”;
}
}

Output:

jsonString: {“name”:”India”,”languages”:[{“name”:”Hindi”,”family”:”Indo-European”},{“name”:”Tamil”,”family”:”Dravidian”}]}

Country [name=India, languages=[Language [name=Hindi, family=Indo-European], Language [name=Tamil, family=Dravidian]]]

Example 2: Array to JSON and back

private void convertArrayToJson()
{
     int[] years = { 1929, 1987, 2000, 2008, 2020 };
     Gson gson = new Gson();

    String jsonString = gson.toJson( years );
    System.out.println( “jsonString: ” + jsonString );

    int[] years1 = gson.fromJson( jsonString, int[].class );
}

Output:

jsonString: [1929,1987,2000,2008,2020]

Example 3: List to JSON and back

private void convertListToJson()
{
    List<String> flowers = Arrays.asList( “Lotus”, “Cereus”, “Rose” );
    Gson gson = new Gson();

    String jsonString1 = gson.toJson( flowers );
    System.out.println( “jsonString: ” + jsonString1 );

    String[] flowersArr = gson.fromJson( jsonString1, String[].class );
    List<String> flowers1 = new ArrayList<String>( Arrays.asList( flowersArr ) );        System.out.println( “flowers1: ” + flowers1 );
}

Output:

jsonString: [“Lotus”,”Cereus”,”Rose”]
flowers1: [Lotus, Cereus, Rose]

Leave a Comment

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