0

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

  • How many pairs will be there in JSON string
  • type-of Value. It can be String or Object.

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,

  • How I can store json in this case efficiently without iterating over JSON String.
  • What should be my Hashmap type, definetily not HashMap <String, String> ?

Thanks in Advance !

Om Sao
  • 7,064
  • 2
  • 47
  • 61
  • 1
    *How I can store json in this case efficiently without iterating over JSON String.* You can't parse JSON without parsing it. – shmosel Sep 20 '17 at 03:29
  • *What should be my Hashmap type, definetily not HashMap ?* How about `Map`? – shmosel Sep 20 '17 at 03:29
  • What's wrong with a `HashMap`? – Henry Sep 20 '17 at 03:30
  • @shmosel: By not iterating, I meant. In 1-liner how can I do like giving jsonString itself as argument. Is there any way to do so ? I also thought of `HashMap` but not sure, how I can use that to insert key-value pair in 1-liner – Om Sao Sep 20 '17 at 03:31
  • Not if you want to split out the key-value pairs. – shmosel Sep 20 '17 at 03:33
  • @shmosel and @Henry: Can you please direct me to link or give an example to use `HashMap` ? – Om Sao Sep 20 '17 at 03:35
  • "I meant. In 1-liner": write a method that does it (just 4 or 5 lines anyhow). It can then be called in 1 line. – Henry Sep 20 '17 at 03:35
  • Example of what? You don't know how to add an entry to a map? – shmosel Sep 20 '17 at 03:36

2 Answers2

5

you can try following code.

List<Map<String, Object>> mapdataList = new ArrayList<Map<String, Object>>();

    Map<String, Object> MapObj = new HashMap<String, Object>();
    MapObj.put("windowHandle", "current");
    MapObj.put("id", "{a67fc38c-10e6-4c5e-87dc-dd45134db570}");
    mapdataList.add(MapObj);

    Gson gson = new Gson();
    JsonObject jObject = new JsonObject();
    JsonParser jP = new JsonParser();
    jObject.add("data", jP.parse(gson.toJson(mapdataList)));
    System.out.println(jObject.toString());
Tejal
  • 764
  • 1
  • 10
  • 39
0

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.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68