How to convert json into POJO in java using jackson

Noam picture Noam · Aug 27, 2012 · Viewed 51.9k times · Source

I'm using spring 3.1.2 and I need to parse a json object into POJO. This is the json that I need to parse:

{
"Person" : {
    "id" : "2"
 },
"Dog" : {
    "dateOfBirth" : "2012-08-20 00:00:00",
    "price" : "10.00"
    }
}

I need to convert this json object (which is combined from two objects) into one POJO, here it is:

public class MyClass{
     public MyClass(){}
     public MyClass(String personsId, TimeStamp dogsDateOfBirth, BigDecimal dogsPrice){
     .... // assign each parameter to the appropriate field
     }
     private String personsId;
     private TimeStamp dogsDateOfBirth;
     private BigDecimal dogsPrice;
     //... Getters and Setters for each field
}

For that matter I used ObjectMapper mapper = new ObjectMapper(); Now since I have several json objects my code looks like this:

    String json = ... ;// A json with several objects as above
    JsonNode tree = mapper.readTree(json);
    Iterator<JsonNode> iter = tree.path("data").getElements();
    while (iter.hasNext()){
        JsonNode node = iter.next();
        MyClass myClass = mapper.readValue(node, MyClass.class);
        ... // do something with myClass object
    }

When I run this - I get the following exception:

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class ...MyClass]: can not instantiate from JSON object (need to add/enable type information?)

I tried to create a simple POJO - Person:

public class Person{
        private String id;          
        public Person(){}
        public Person(String id){
            this.id = id;
         }
         ... // Getter and Setter
    }

and do the following:

Person person = mapper.readValue(node.path("Person"), Person.class);

I get this (same) exception:

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class ...Person]: can not instantiate from JSON object (need to add/enable type information?)

I tried to read some about the type information - but couldn't understand how it can help me here.

How can I convert this json into my POJO?

Thanks.

Answer

Noam picture Noam · Aug 27, 2012

What I did was this: I created a new class that holds a Person object and Dog object, these classes needs to be static ( I found it here ). Here are the classes:

public static class MyNewClass{
    private Person person;
    private Dog dog;
    ... // two constructors and getters and setters

 public static class Person{
     private String id;
     ... // two constructors and getters and setters
 }
 public static class Dog{
     private String dateOfBirth;
     private String price;
     ... // two constructors and getters and setters
  }
}

Now my code looks like this:

    JsonNode tree = mapper.readTree(jsonString);
    Iterator<JsonNode> iter = tree.path("data").getElements();
    while (iter.hasNext()){
        JsonNode node = iter.next();
        Person person = mapper.readValue(node.path("Person"), Person.class);
        Dog dog = mapper.readValue(node.path("Dog"), Dog.class);
        MyNewClass myNewClass = new MyNewClass(person , dog);
        ... //Do something with it
    }

I still want to do it without creating these two objects ( Person and Dog ) - That'a good enough for now - but if someone have an idea - I would like to here!

Thanks.