This is possibly a duplicate question.. please refer this link.
I am able to map one json object to POJO.But how can i convert array of json object to pojo using the same jackson framework.
private void jsonToPojo(){
ObjectMapper mapper=new ObjectMapper();
try {
User1 user1=mapper.readValue(readFromFile(), User1.class);
User1[] user2=mapper.readValue(readFromFile(), User1[].class);
System.out.println(user1);
Toast.makeText(getApplicationContext(), "inside try", 0).show();
} catch (JsonParseException e) {
// TODO Auto-generated catch block
Log.i("Exception", "jsonparseexception");
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
Log.i("Exception", "jsonmapping exception");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("Exception", "ioexception");
e.printStackTrace();
}
}
This is the class user object.
public class User {
private int age = 23;
private String name = "amal";
private List<String> messages = new ArrayList<String>() {
{
add("hi");
add("how");
add("are u.");
}
};
//getter and setter methods
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + ", " +
"messages=" + messages + "]";
}
This is what i tried to do:(readFromFile() takes json from a file)
User1[] user2=mapper.readValue(readFromFile(), User1[].class);
The jsonToPojo() is working well for only one object. However, if i try the above line of code,its not taking the following json:
[
{
"age":"23",
"messages":["hi","how","are u."],
"name":"amal"
},
{
"age":"98",
"messages":["Reply","my","question"],
"name":"You"
}
]