How to convert json into POJO in java using jackson How to convert json into POJO in java using jackson json json

How to convert json into POJO in java using jackson


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.


The problem is the same as described here : Jackson error: no suitable constructor

The class that you're trying to instantiate is not static. Because of that, it has a hidden constructor parameter. This is causing Jackson to fail.


Try changing the constructor of MyClass to accept String for all the fields, since all the fields are strings in your JSON data. BTW, there is no standard representation for TimeStamps in JSON, so you will need to do a conversion for date fields anyway. For the "price" field, you could try changing

"price" : "10.00"

to

"price" : 10.00

in the JSON data; that should allow it to be read as BigDecimal.