0% found this document useful (0 votes)
52 views10 pages

Java Gson + JSON Tutorial With Examples: Gson Jar To Resolve Dependency

This document provides a tutorial on using the Gson Java library to convert between Java objects and JSON strings. It discusses how to create Gson objects, convert a JSON string to a Java object using fromJson(), convert a Java object to a JSON string using toJson(), and pretty print JSON output. Code examples are provided to demonstrate converting a Person object with nested Address and array properties between JSON and Java representations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views10 pages

Java Gson + JSON Tutorial With Examples: Gson Jar To Resolve Dependency

This document provides a tutorial on using the Gson Java library to convert between Java objects and JSON strings. It discusses how to create Gson objects, convert a JSON string to a Java object using fromJson(), convert a Java object to a JSON string using toJson(), and pretty print JSON output. Code examples are provided to demonstrate converting a Person object with nested Address and array properties between JSON and Java representations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

15/3/2016 Java Gson + JSON Tutorial with Examples

Java Gson + JSON Tutorial with Examples
By Arvind Rai, July 21, 2014

Gson is google API that effectively coverts JSON into corresponding java object and java object into JSON string. Gson

provides options to print JSON in well­defined way. In our example I will provide a complete demo how to use Gson.

We will see how to create Gson object and how API can be used to convert between java object and JSON.

Gson Jar to Resolve Dependency
Resolve project dependency either by directly downloading gson­2.2.4.jar or use maven as below.

  <dependency> 

    <groupId>com.google.code.gson</groupId> 

    <artifactId>gson</artifactId> 

    <version>2.2.4</version> 

        </dependency>

Ways To Create GSON Object
There are two way to create Gson object.

1. This is the simple way and we get all default settings for Gson.

http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 1/10
15/3/2016 Java Gson + JSON Tutorial with Examples

Gson gson = new Gson(); 

2. GsonBuilder is also used to get instance of Gson that facilitate to set other settings. Suppose we want pretty

printing of Json, then we can create Gson object as below.

Gson gson = new GsonBuilder().setPrettyPrinting().create(); 

From JSON to Java Object Using GSON API
Gson has fromJson() method that accepts JSON string and a class. This method converts JSON into the object of

specified class. In our example we have a person data as JSON and person class. Find the JSON string which we will

use to create Peron object. 

Person JSON

{  'id': 10, 

   'name': 'Ram',  

    'address':  

    {'city': 'Varanasi', 

     'zip': 221001 

    }, 

    'mobileNums': 

     [ 111111,  

http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 2/10
15/3/2016 Java Gson + JSON Tutorial with Examples

       222222  

     ] 

 } 

Find the Address class used in Person class. 

Address.java

package com.concretepage.gson; 

public class Address { 

    private String city; 

    private String zip; 

  public String getCity() { 

    return city; 

  } 

  public void setCity(String city) { 

    this.city = city; 

  } 

  public String getZip() { 

    return zip; 

  } 

http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 3/10
15/3/2016 Java Gson + JSON Tutorial with Examples

  public void setZip(String zip) { 

    this.zip = zip; 

  } 

Now find the person clas. Person class has all fields for person JSON. Person class field name and JSON key name must

match so that Gson can find the field name to set the value from JSON. 

Person.java

package com.concretepage.gson; 

public class Person { 

    private int id; 

    private String name; 

    private Address address; 

    private long[] mobileNums; 

  public int getId() { 

    return id; 

  } 

  public void setId(int id) { 

    this.id = id; 

http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 4/10
15/3/2016 Java Gson + JSON Tutorial with Examples

  } 

  public String getName() { 

    return name; 

  } 

  public void setName(String name) { 

    this.name = name; 

  } 

  public Address getAddress() { 

    return address; 

  } 

  public void setAddress(Address address) { 

    this.address = address; 

  } 

  public long[] getMobileNums() { 

    return mobileNums; 

  } 

  public void setMobileNums(long[] mobileNums) { 

    this.mobileNums = mobileNums; 

  } 

http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 5/10
15/3/2016 Java Gson + JSON Tutorial with Examples

Now find the main class which will use Gson API to convert JSON into person Object. 

JsonToJavaObject.java

package com.concretepage.gson; 

import com.google.gson.Gson; 

public class JsonToJavaObject { 

  public static void main(String[] args) { 

    String jsonStr= "{  'id': 10, 'name': 'Ram', 'address': {'city': 'Varanasi','zip':" + 

        " 221001 },'mobileNums': [ 111111, 222222 ]}"; 

    Gson gson = new Gson(); 

    Person person = gson.fromJson(jsonStr, Person.class); 

    System.out.println(person.getName()); 

    System.out.println(person.getAddress().getCity()); 

    long mobNums[] = person.getMobileNums(); 

    System.out.println(mobNums[0]); 

  } 

Now person object has all data which been declared in JSON format. Find the output for some person field.
http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 6/10
15/3/2016 Java Gson + JSON Tutorial with Examples

Ram 

Varanasi 

111111 

From Java Object to JSON using GSON API
Gson has toJson() method that accepts an object and returns the corresponding JSON string. In our example we will

create a person object and then we will print its JSON format.

JavaObjectToJson.java

package com.concretepage.gson; 

import com.google.gson.Gson; 

public class JavaObjectToJson { 

  private static void createPerson(Person person){ 

    person.setId(15); 

    person.setName("Shyam"); 

    Address address = new Address(); 

    address.setCity("Allahabad"); 

    address.setZip("230043"); 

    person.setAddress(address); 

http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 7/10
15/3/2016 Java Gson + JSON Tutorial with Examples

    long[] mobNums = {33333,44444}; 

    person.setMobileNums(mobNums); 

  } 

  public static void main(String[] args) { 

    Person person = new Person(); 

    Gson gson = new Gson(); 

    createPerson(person); 

    System.out.println(gson.toJson(person)); 

  } 

Find the output as JSON from person java object.

{"id":15,"name":"Shyam","address":{"city":"Allahabad","zip":"230043"},"mobileNums":[33333,44444]} 

Pretty Printing for JSON Using Gson
When we create instance of Gson using GsonBuilder , we can set different format settings. Gson by default prints

compact output of JSON which will be in one line. We can set Pretty Printing and the Gson will print JSON in well

formatted. Change main method as below in JavaObjectToJson.java

    public static void main(String[] args) { 

http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 8/10
15/3/2016 Java Gson + JSON Tutorial with Examples

      Person person = new Person(); 

      Gson gson = new GsonBuilder().setPrettyPrinting().create(); 

      createPerson(person); 

      String jsonStr = gson.toJson(person); 

      System.out.println(jsonStr); 

    } 

Find JSON pretty printing from person java object.

  "id": 15, 

  "name": "Shyam", 

  "address": { 

    "city": "Allahabad", 

    "zip": "230043" 

  }, 

  "mobileNums": [ 

    33333, 

    44444 

  ] 

http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 9/10
15/3/2016 Java Gson + JSON Tutorial with Examples

http://www.concretepage.com/google­api/java­gson­json­tutorial­examples 10/10

You might also like