Wednesday 28 October 2015

Jackson: Convert JSON to map

By using TypeReference , we can map JSON data to a map.
Map<String, Employee> map = mapper.readValue(json, new TypeReference<Map<String, Employee>>() {});

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 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.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
  public static void main(String args[]) throws JsonGenerationException,
      JsonMappingException, IOException {
    Employee emp1 = new Employee();
    emp1.setFirstName("Hari Krishna");
    emp1.setId(1);
    emp1.setLastName("Gurram");
    emp1.getHobbies().add("Trekking");
    emp1.getHobbies().add("Blogging");
    emp1.getHobbies().add("Cooking");

    Employee emp2 = new Employee();
    emp2.setId(2);
    emp2.setFirstName("Sankalp");
    emp2.setLastName("Dubey");
    emp2.getHobbies().add("Listeninig to Music");
    emp2.getHobbies().add("Car driving");

    Map<String, Employee> emps = new HashMap<>();
    emps.put("1", emp1);
    emps.put("2", emp2);

    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(emps);

    System.out.println(json);

    System.out.println("Deserializing JSON to Map");

    Map<String, Employee> map = mapper.readValue(json,
        new TypeReference<Map<String, Employee>>() {
        });

    for (String key : map.keySet()) {
      Employee emp = map.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","Cooking"]},"2":{"id":2,"firstName":"Sankalp","lastName":"Dubey","hobbies":["Listeninig to Music","Car driving"]}}
Deserializing JSON to Map
1 Hari Krishna Gurram [Trekking, Blogging, Cooking]
2 Sankalp Dubey [Listeninig to Music, Car driving]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment