Java Gson + JSON Tutorial
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.
Gson gson = new Gson();
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, 222222 ] }
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; } public void setZip(String zip) { this.zip = zip; } }
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; } 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; } }
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]); } }
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); 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)); } }
{"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.javapublic static void main(String[] args) { Person person = new Person(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); createPerson(person); String jsonStr = gson.toJson(person); System.out.println(jsonStr); }
{ "id": 15, "name": "Shyam", "address": { "city": "Allahabad", "zip": "230043" }, "mobileNums": [ 33333, 44444 ] }