Apr 11

Convert json string to Map in Java using Gson

I’ve been using Gson from quite some time now. It’s a Java library developed by Google that is used to convert JSON to Java objects and vice versa. You can read more about Gson here.

I came across this json string which I had to convert into a Map. Here’s how you do it:

Type type =
 new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> map =
 gson.fromJson("{'key1':'123','key2':'456'}", type);

Hope that helps you convert a json string into a java Map.