I'm writing RESTful web service for my android app. I have my POJO class named ServiceResponse:
private int responseCode;
private String objectType;
private ArrayList<Object> data;
public int getResponseCode() {
return responseCode;
}
public void setResponseCode(int responseCode) {
this.responseCode = responseCode;
}
public ArrayList<Object> getData() {
return data;
}
public void setArrayData(ArrayList<Object> data) {
this.data = data;
}
public void setData(Object data) {
if(this.data == null) this.data = new ArrayList<Object>();
this.data.add(data);
}
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
It's parsed to JSON using Jackson, example here: http://pastebin.com/pu8792a1 There will be many classes passed using this arraylist. My question is, how do I make it ServiceResponse object again from the JSON?
{
"responseCode" : 2,
"objectType" : "com.example.User",
"data" : [ {
"name" : "name",
"password" : "pass",
"email" : "mail@gmail.com",
"device" : "ABC",
"level" : 10,
"gold" : 82,
"delois" : 0,
"uranium" : 0,
"attack" : 5,
"speed" : 5,
"armor" : 5,
"controllability" : 5,
"exp" : 100,
"hp" : 100,
"hpMax" : 300,
"deuter" : 21,
"deuterMax" : 120,
"research" : 1413360596907,
"id" : 1
} ]
I tried: (skipped try & catch)
ObjectMapper mapper = new ObjectMapper();
ServiceResponse response;
response = mapper.readValue(output, ServiceResponse.class);
User user = (User) ( response.getData().get(0) );
System.out.println(user.toString());
but I got ClassCastException: java.util.ArrayList cannot be cast into com.example.User