How to use Jackson to deserialise an array of objects

前端 未结 8 1284
梦谈多话
梦谈多话 2020-11-21 22:58

The Jackson data binding documentation indicates that Jackson supports deserialising \"Arrays of all supported types\" but I can\'t figure out the exact syntax for this.

8条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 23:20

    I was unable to use this answer because my linter won't allow unchecked casts.

    Here is an alternative you can use. I feel it is actually a cleaner solution.

    public  List parseJsonArray(String json, Class clazz) throws JsonProcessingException {
      var tree = objectMapper.readTree(json);
      var list = new ArrayList();
      for (JsonNode jsonNode : tree) {
        list.add(objectMapper.treeToValue(jsonNode, clazz));
      }
      return list;
    }
    

提交回复
热议问题