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.
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}