Table of Contents
In this post, we will see about Gson fromJson example.We have already seen simple Gson example in the previous post.
Gson fromJson method is used to convert JSON String or JsonObject or Reader to the corresponding object. You can read about various variants about fromJson method over Gson page.
I will demonstrate Gson fromJson method with help of example.
Read simple JSON String
You can use fromJson(String json, Class classOfT) method to read simple Json string.
Let’s say you have Country class as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; public class Country { int id; String countryName; long population; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } @Override public String toString() { return "Country [id=" + id + ", countryName=" + countryName + ", population=" + population + "]"; } } |
Create a class named "SimpleGsonMain"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import com.google.gson.Gson; public class SimpleGsonMain { public static void main(String[] args) { String jsonString="{'id'=1,countryNasme='India',population=20000}"; Gson gson=new Gson(); Country country=gson.fromJson(jsonString, Country.class); System.out.println(country.toString()); } } |
When you run above program, you will get below output.
Read JSON Array to List
Let’s say you need to read below JSON string from file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[ { "id":1, "countryName":"India", "population":20000 }, { "id":2, "countryName":"Nepal", "population":12000 }, { "id":3, "countryName":"Bhutan", "population":6000 }, { "id":4, "countryName":"China", "population":30000 } ] |
We need to convert above JSON Array to List<Country>
You won’t be simply able to do it using below code.
1 2 3 |
List<Country> listOfCountries=gson.fromJson(jsonString, ArrayList.class); |
Above code won’t work. If object is ParameterizedType type, then you need to use type to convert it.
Hence, you can use fromJson(Reader json, Type typeOfT) to read above json file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package org.arpit.java2blog; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.Reader; import java.util.List; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonReadFileToListMain { public static void main(String[] args) { Reader reader; try { reader = new FileReader("/Users/apple/Desktop/Countries.json"); Gson gson=new Gson(); // Usinf fromJson(reader,type) List<Country> countryList=gson.fromJson(reader,new TypeToken<List<Country>>(){}.getType()); System.out.println("======================="); System.out.println("List of Countries are:"); System.out.println("======================="); for (Country country : countryList) { System.out.println(country); } } catch (FileNotFoundException e) { e.printStackTrace(); } } } |
When you run above program, you will get below output:
List of Countries are:
=======================
Country [id=1, countryName=India, population=20000] Country [id=2, countryName=Nepal, population=12000] Country [id=3, countryName=Bhutan, population=6000] Country [id=4, countryName=China, population=30000]
Read Json String and convert it to Map
Let’s say we have flat gson string and we want to put it in a Map. You can simply use fromJson(String json, Type typeOfT) and convert String to Map as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.util.Map; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonFromJsonToMapMain { public static void main(String args[]) { Gson gson=new Gson(); String counrtyCapitalJson="{'India':'Delhi','Nepal':'Kathmandu','China':'Beijing','Bhutan':'Thimphu'}"; Map<String,String> map=gson.fromJson(counrtyCapitalJson,new TypeToken<Map<String,String>>(){}.getType()); for(String key:map.keySet()) { System.out.println(map.get(key)+" is capital of "+key); } } } |
When you run above program, you will get below output:
Kathmandu is capital of Nepal
Beijing is capital of China
Thimphu is capital of Bhutan
As you can see we have provided TypeToken to convert it specific type.
That’s all about Gson fromJson example.