Wednesday 28 October 2015

Gson: Convert Json to a HashMap

In this post, I am going to explain how to convert Json to HashMap.

Following statements are used to convert json to map.

Type type = new TypeToken<Map<String, Employee>>(){}.getType();
Map<String, Employee> result = gson.fromJson(json,type);

Following is the complete working application

import java.util.ArrayList;
import java.util.List;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private List<String> hobbies = new ArrayList<>();

 public Employee() {
  super();
 }

 public Employee(int id, String firstName, String lastName) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public List<String> getHobbies() {
  return hobbies;
 }

 public void setHobbies(List<String> hobbies) {
  this.hobbies = hobbies;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=")
    .append(firstName).append(", lastName=").append(lastName)
    .append(", hobbies=").append(hobbies).append("]");
  return builder.toString();
 }

}


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 Main {
 public static void main(String args[]) {
  Gson gson = new Gson();

  Employee emp1 = new Employee(1, "Hari Krishna", "Gurram");
  Employee emp2 = new Employee(2, "Sandeep", "Kishore");

  emp1.getHobbies().add("Trekking");
  emp1.getHobbies().add("Blogging");

  emp2.getHobbies().add("Shopping");
  emp2.getHobbies().add("Bike riding");

  Map<String, Employee> map = new HashMap<>();

  map.put("1", emp1);
  map.put("2", emp2);

  String json = gson.toJson(map);
  System.out.println(json);

  System.out.println("deserializing json to map ");

  Type type = new TypeToken<Map<String, Employee>>() {
  }.getType();
  Map<String, Employee> result = gson.fromJson(json, type);

  for (String key : result.keySet()) {
   Employee emp = result.get(key);
   System.out.println(emp.getId() + " " + emp.getFirstName() + " "
     + emp.getLastName() + " " + emp.getHobbies());
  }
 }
}


Output

{"1":{"id":1,"firstName":"Hari Krishna","lastName":"Gurram","hobbies":["Trekking","Blogging"]},"2":{"id":2,"firstName":"Sandeep","lastName":"Kishore","hobbies":["Shopping","Bike riding"]}}
deserializing json to map 
1 Hari Krishna Gurram [Trekking, Blogging]
2 Sandeep Kishore [Shopping, Bike riding]






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment