This tutorial covers How to Parse Json in Java. You will learn to use Gson API to convert JSON Strings into Java Objects with the helps of examples.
A guide on How to use Gson API to Parse JSON Strings to Java Objects. Covers examples of Converting JSON to Objects and Maps.
To Learn more:
Tutorial Contents
Setup
Let’s begin with setting up the essentials. In order to use Gson API, we will have to add it as a dependency. Also, we will create a Java Pojo class that will be used during the examples.
Gson Dependency
Use latest version of Gson API.
pom.xml
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>{version}</version> </dependency>
Or, in build.gradle
compile group: 'com.google.code.gson', name: 'gson', version: '<version>'
Java POJO Class
We will create Account.java class to use it for the parsing of JSON strings.
package com.amitph.spring.tutorials.students.model; public class Account { private long accountNumber; private String accountHolder; private String type; private double balance; public long getAccountNumber() { return accountNumber; } public void setAccountNumber(long accountNumber) { this.accountNumber = accountNumber; } public String getAccountHolder() { return accountHolder; } public void setAccountHolder(String accountHolder) { this.accountHolder = accountHolder; } public String getType() { return type; } public void setType(String type) { this.type = type; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } }
JSON to Object using Gson
Let’s try an example of using Gson to Parse Json String into an Object.
String json = """ { "accountNumber":1234, "accountHolder":"Strong Belwas", "type":"Savings", "balance":1239.39 } """; Gson gson = new Gson(); Account account = gson.fromJson(json, Account.class); System.out.println(account);
Here, we are using Java Text Blocks to hold multiline JSON String.
JSON to Map using Gson
In order to convert JSON into a Map, which has a generic type we need to create a Type
instance.
Next example demonstrates using Gson to parse JSON string to a Map.
String json = """ { "accountNumber":1234, "accountHolder":"Strong Belwas", "type":"Savings", "balance":1239.39 } """; Gson gson = new Gson(); Type type = new TypeToken<Map<String, String>>() {}.getType(); Map<String, String> account = gson.fromJson(json, type);
JSON to Map of Objects using Gson
Similarly, we can cast a JSON to Map of custom objects by using correct Type
.
String json = """ { "1234":{ "accountNumber":1234, "accountHolder":"Strong Belwas", "type":"Savings", "balance":1239.39 } } """; Gson gson = new Gson(); Type type = new TypeToken<Map<Long, Account>>() {}.getType(); Map<Long, Account> account = gson.fromJson(json, type);
JSON to Array of Objects using Gson
The JSON String is an array of Students we can map it to Student[]
.
String json = """ [{ "accountNumber":1234, "accountHolder":"Strong Belwas", "type":"Savings", "balance":1239.39 }] """; Gson gson = new Gson(); Student[] students = gson.fromJson(json, Student[].class);
Summary
In this short tutorial we have learnt How to use Gson API. We have covered examples to parse JSON Strings into Java Objects, Maps, and Arrays.
For more on Java please visit : Java Tutorials