I have a JSON in string format coming from DB, about which I only know that it's in key-value format. I don't know beforehand
Here are some of my JSON String:
{"windowHandle":"current"} \\ in one String: String format
{"type":"implicit","ms":100000} \\ in two String: String format
{"id":"{a67fc38c-10e6-4c5e-87dc-dd45134db570}"} \\ in one String: String format
{"key1": {"sub-key1": "sub-value1", "sub-key2": "sub-value2"}, "key2": {"sub-key2": "sub-value2"}} \\ in two String: Object format
So, basically: Number of key-value pair and type of value(String, Object) is not known beforehand.
I want to store this data into my Hashmap. I know that if could be only String: String format, I could do as following put
in the Hashmap.
My question is,
HashMap <String, String>
?Thanks in Advance !
You can use the com.fasterxml.jackson.databind.ObjectMapper
or Gson
to convert the String into a Map<String, Object>
.
Here is an example using Gson
public static void main(String[] args) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Map map = gson.fromJson("{\"var\":\"value\"}", Map.class);
System.out.println("map = " + map);
}
You can get an example of the ObjectMapper
in action over here.