Convert JsonNode into POJO

后端 未结 4 1019
梦毁少年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条回答
  •  旧时难觅i
    2020-11-30 21:01

    If you're using org.codehaus.jackson, this has been possible since 1.6. You can convert a JsonNode to a POJO with ObjectMapper#readValue: http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/ObjectMapper.html#readValue(org.codehaus.jackson.JsonNode, java.lang.Class)

    
        ObjectMapper mapper = new ObjectMapper();
        JsonParser jsonParser = mapper.getJsonFactory().createJsonParser("{\"foo\":\"bar\"}");
        JsonNode tree = jsonParser.readValueAsTree();
        // Do stuff to the tree
        mapper.readValue(tree, Foo.class);
    

提交回复
热议问题