How to use Jackson to deserialise an array of objects

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

    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory f = new JsonFactory();
        List lstUser = null;
        JsonParser jp = f.createJsonParser(new File("C:\\maven\\user.json"));
        TypeReference> tRef = new TypeReference>() {};
        lstUser = mapper.readValue(jp, tRef);
        for (User user : lstUser) {
            System.out.println(user.toString());
        }
    
    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题