tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > GOOGLE GSON > JSON to Map

JSON to Map 

Google-gson is a java library from Google for encoding and decoding JSON text. Get the latest binaries from http://code.google.com/p/google-gson/. The following example shows converting a JSON string to java map.

File Name  :  
com/bethecoder/tutorials/google_gson/tests/Json2Map.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.google_gson.tests;

import java.lang.reflect.Type;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Json2Map {

  /**
   @param args
   */
  public static void main(String[] args) {
    Gson gson = new Gson();
    String jsonStr = "{\"firstName\":\"Sriram\"," +
        "\"lastName\":\"Kasireddi\"," +
        "\"age\":2," +
        "\"hobby\":\"Singing\"," +
        "\"dob\":\"May 6, 2010 12:00:00 AM\"}";
    
    Type mapType = new TypeToken<Map<String, String>>() {}.getType();
    Map map = gson.fromJson(jsonStr, mapType);
    System.out.println(map);   
  }
}
   

It gives the following output,
{firstName=Sriram, lastName=Kasireddi, age=2, hobby=Singing, dob=May 6, 2010 12:00:00 AM}



 
  


  
bl  br