Convert JsonNode into POJO

后端 未结 4 983
梦毁少年i
梦毁少年i 2020-11-30 20:24

This may seem a little unusual, but I am looking for an efficient way to transform/map a JsonNode into a POJO.

I store some of my Model\'s

4条回答
  •  离开以前
    2020-11-30 21:03

    String jsonInput = "{ \"hi\": \"Assume this is the JSON\"} ";
    com.fasterxml.jackson.databind.ObjectMapper mapper =
        new com.fasterxml.jackson.databind.ObjectMapper();
    MyClass myObject = objectMapper.readValue(jsonInput, MyClass.class);
    

    If your JSON input in has more properties than your POJO has and you just want to ignore the extras in Jackson 2.4, you can configure your ObjectMapper as follows. This syntax is different from older Jackson versions. (If you use the wrong syntax, it will silently do nothing.)

    mapper.disable(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNK‌​NOWN_PROPERTIES);
    

提交回复
热议问题