I am using spring 3.1.2 and I need to parse a json object in POJO. This is the json 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){ ....
In this regard, 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; } ...
and follow these steps:
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 information about - but could not figure out how this could help me.
How can I convert this json to my POJO?
Thanks.