-1

I am trying to find a way to convert JSON into Map where key is string and value will be List of objects.

Example JSON:

{
  "name1": [
    {
      "host": "host1",
      "isEnabled": true
    },
    {
      "host": "host2",
      "isEnabled": false
    }
  ],
  "name2": [
    {
      "host": "host1",
      "isEnabled": true
    },
    {
      "host": "host2",
      "isEnabled": false
    }
  ]
}

In above example "name1" will be the key and value will be list of object(that object will have "host" and "isEnabled" field).

I want to read this json convert in Map in java. Can someone help me in achieving this.

newbie
  • 1
  • 2

1 Answers1

0

You can use jackson with TypeReference like below -

 ObjectMapper mapper = new ObjectMapper();
    Map<String, ArrayList<Type>> map = mapper.readValue(msg,new TypeReference<HashMap<String, ArrayList<Type>>>(){});

where Type is pojo as per your message like below -

public class Type {
public String host;
public boolean isEnabled;
}