Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert json into POJO in java using jackson

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.

like image 964
Noam Avatar asked Aug 27 '12 09:08

Noam


People also ask

How do you convert JSON to Java object?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.

What converts JSON to and from POJO using property accessor?

Data Binding API is used to convert JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations.


1 Answers

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.

like image 88
Noam Avatar answered Oct 19 '22 04:10

Noam