27

I have a JSON of the format

[{
    "id" : "a01",
    "name" : "random1",
    "val" : "random2"

},
{
    "id" : "a03",
    "name" : "random3",
    "val" : "random4"
}]

I need to map it to a List holding various Map objects. How do I achieve it?

Even if I am able to convert this JSON to a List of String of the form

{
    "id" : "a01",
    "name" : "random1",
    "val" : "random2"

}

then I have a method to convert each individual String to a Map.

nabster
  • 1,561
  • 2
  • 20
  • 32
Pranay Kumar
  • 400
  • 1
  • 3
  • 12
  • using jackson data-bind ? you can use guava to achieve it https://github.com/google/gson – LazerBanana Jun 22 '17 at 11:45
  • Could you please guide me here - https://stackoverflow.com/questions/70907506/how-to-get-the-flux-response-into-map? – PAA Jan 29 '22 at 16:48

3 Answers3

61

You will need to pass a TypeReference to readValue with the desired result type:

ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> data = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
2

Use gson with specified type to convert to list of maps:

Gson gson = new Gson();
Type resultType = new TypeToken<List<Map<String, Object>>>(){}.getType();
List<Map<String, Object>> result = gson.fromJson(json, resultType);
alexey28
  • 5,170
  • 1
  • 20
  • 25
-1

private List<Map<String, Object>> map_string;

Json you need to pass:-

{
"map_formula" : [ 
        {
            "A+" : "if(price<400),\"40000\",0",
            "B" : "",
            "c" : "",
            "d" : "",
            "e" : ""
        }, 
        {
            "poor" : "value for poor",
            "good" : "300",
            "average" : "300",
            "excellent" : "300"
        }
    ]
}