JSON Map To List <Map << String, Object >>

I have json format

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

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

      

I need to hide the list of cards. So how can I get the same thing? Even though I can convert it to a list of strings, so that each string is of the form

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

}

      

I have a way to convert it to a map. So how do you retrieve such a list of strings?

+3


source share


2 answers


You need to pass TypeReference

in readValue

with the desired result type:



ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> data = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});

      

+7


source


Use gson with the specified type to convert to a 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);

      

+2


source







All Articles