Gson fromJson example

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.

Create a class named "SimpleGsonMain"

When you run above program, you will get below output.

Country [id=1, countryName=null, population=20000]

Read JSON Array to List

Let’s say you need to read below JSON string from file.

We need to convert above JSON Array to List<Country>
You won’t be simply able to do it using below code.

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.

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:

When you run above program, you will get below output:

Delhi is capital of India
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.

Related Posts

  • 11 August

    Create A REST API With JSON Server

    In this tutorial, we will see about JSON server. Let’s say you are front-end developer and need to integrate backend calls with REST APIs but you came to know from backend team that REST APIs are not completed yet and will take some time to do the coding. How will you proceed now? JSON server […]

  • 04 October

    Jackson Streaming API – read and write json

    Table of ContentsJava JSON Tutorial Content:Write JSON to file:Read JSON to file: In this post,we will see how can we will use jackson streaming API to read and write JSON . Java JSON Tutorial Content: JSON Introduction JSON.simple example-read and write JSON GSON example-read and write JSONJackson example – read and write JSON Jackson Streaming […]

  • 04 October

    Jackson example – Read and write JSON

    Table of ContentsJava JSON Tutorial Content:Write JSON to file:Write formatted JSON to file:Read JSON to file: In this post,we will see how can we read and write JSON using Jackson. Java JSON Tutorial Content: JSON Introduction JSON.simple example-read and write JSONGSON example-read and write JSONJackson example – read and write JSONJackson Streaming API – read […]

  • 17 November

    GSON example – Read and write JSON

    In this post,we will see how can we read and write JSON using GSON. Java JSON Tutorial Content: JSON Introduction JSON.simple example-read and write JSON GSON example-read and write JSON Jackson example – read and write JSON Jackson Streaming API – read and write JSON reading and writing JSON using json-simple.We will use another way(i.e. […]

  • 17 November

    JSON.simple example – Read and write JSON

    In this post,we will see how can we read and write JSON using json.simple. Java JSON Tutorial Content: JSON Introduction JSON.simple example-read and write JSON GSON example-read and write JSON Jackson example – read and write JSON Jackson Streaming API – read and write JSON JSON.simple, is a simple Java library for JSON processing, read […]

  • 17 November

    Java JSON tutorial

    In this post,We will see introduction to JSON Java JSON Tutorial Content: JSON Introduction JSON.simple example-read and write JSON GSON example-read and write JSON Jackson example – read and write JSON Jackson Streaming API – read and write JSON What is JSON? JSON stands for JavaScript Object Notation.It  is a simple and easy to read […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.