Search code examples
javajsonpojo

Generating POJO instances from Json


Is there a lib to create POJO instance from Json ?

Actually I'm using JJSchema to generate a full Json from the POJO but I can't find to do the reverse.

PS : My POJO is have other POJOs as attributes.


Solution

  • You should give FasterXML Jackson a try. In my opinion it is very straight forward and easy to use. Here's the example from the github page that describes how a Java Object can be created from JSON

    ObjectMapper mapper = new ObjectMapper(); // create once, reuse
    MyValue value = mapper.readValue(new File("data.json"), MyValue.class);
    // or:
    value = mapper.readValue(new URL("http://some.com/api/entry.json"), MyValue.class);
    // or:
    value = mapper.readValue("{\"name\":\"Bob\", \"age\":13}", MyValue.class);