How to use Jackson to deserialise an array of objects

前端 未结 8 1287
梦谈多话
梦谈多话 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:25

    For Generic Implementation:

    public static  List parseJsonArray(String json,
                                             Class classOnWhichArrayIsDefined) 
                                             throws IOException, ClassNotFoundException {
       ObjectMapper mapper = new ObjectMapper();
       Class arrayClass = (Class) Class.forName("[L" + classOnWhichArrayIsDefined.getName() + ";");
       T[] objects = mapper.readValue(json, arrayClass);
       return Arrays.asList(objects);
    }
    

提交回复
热议问题