Gson – convert JSON to HashMap example shows how to convert JSON to HashMap using the Gson library in Java. The example also shows how to convert JSON to HashMap of custom objects.
How to convert JSON to a Java HashMap?
Let’s start with the below given simple JSON.
1 2 3 4 5 |
{ "rollNumber": 1, "name": "Bob", "marks": 90 } |
In order to convert this student JSON to a HashMap, we need to use the fromJson
method of the Gson class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javacodeexamples.gsonexamples; import java.util.HashMap; import com.google.gson.Gson; public class GsonJSONToHashMapExample { public static void main(String[] args) { String jsonData = "{\"rollNumber\": 1, \"name\": \"Bob\", \"marks\": 90}"; //create Gson object Gson gson = new Gson(); //read JSON to a java HashMap HashMap<String, Object> mapStudent = gson.fromJson(jsonData, HashMap.class); System.out.println(mapStudent); } } |
Output
1 |
{rollNumber=1.0, name=Bob, marks=90.0} |
As you can see from the output, the JSON property name becomes the key of the HashMap, and the value becomes the respective value of the HashMap.
The above example deals with a very simple JSON structure. In the real world, JSON may be much more complex than this. Consider the below given JSON.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{ "student1": { "rollNumber": 1, "name": "Bob", "marks": 90 }, "student2": { "rollNumber": 2, "name": "John", "marks": 90 }, "student3": { "rollNumber": 3, "name": "Rama", "marks": 90 } } |
The above JSON object has student objects as properties. We want our HashMap to contain the property name as a map key and the student object as a map value. Lets parse this JSON to a HashMap.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
package com.javacodeexamples.gsonexamples; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonJSONToHashMapExample { public static void main(String[] args) { String jsonData = "{" + "\"student1\": {\"rollNumber\": 1, \"name\": \"Bob\", \"marks\": 90}," + "\"student2\": {\"rollNumber\": 2, \"name\": \"John\", \"marks\": 90}," + "\"student3\": {\"rollNumber\": 3, \"name\": \"Rama\", \"marks\": 90}" + "}"; //create Gson object Gson gson = new Gson(); //create a type Type studentMapType = new TypeToken<HashMap<String, Student>>() {}.getType(); //read JSON to a java HashMap HashMap<String, Student> mapStudents = gson.fromJson(jsonData, studentMapType); System.out.println("There are " + mapStudents.size() + " students in map"); //iterate over key & value of HashMap for(Map.Entry<String, Student> entry : mapStudents.entrySet()) { System.out.println( entry.getKey() + " => " + "[" + entry.getValue().getRollNumber() + ", " + entry.getValue().getName() + ", " + entry.getValue().getMarks() + "]" ); } } } class Student{ private int rollNumber; private String name; private int marks; public int getRollNumber() { return rollNumber; } public void setRollNumber(int rollNumber) { this.rollNumber = rollNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } } |
Output
1 2 3 4 |
There are 3 students in map student1 => [1, Bob, 90] student2 => [2, John, 90] student3 => [3, Rama, 90] |
As we can see from the output, each student property of the JSON has become the map’s value.
Please let me know your views in the comments section below.