I am trying to store a json array response into an arraylist. Here is what the JSON response looks like:
"identityList":{
"identity":[
{
"firstName":"MICHAEL",
"lastName":"JAMESON",
"gender":"MALE",
"dateOfBirth":"1961-05-18T00:00:00.000+0000",
},
{
"firstName":"KELLY",
"lastName":"JAMESON",
"gender":"FEMALE",
"dateOfBirth":"1951-04-01T00:00:43.000+0000",
}
]
}
Here's the Identity
class:
public class Identity {
/** The first name. */
private String firstName;
/** the middleName. */
private String middleName;
/** the lastName. */
private String lastName;
/** the dateOfBirth. */
private LocalDate dateOfBirth;
public Identity(String firstName, String middleName, String lastName, LocalDate dateOfBirth) {
super();
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
}
Based on what I've found on other SO posts like here I wrote this function to parse it:
public static <T> T getResponseObjectAsArray(String resourceResponse, String jsonObject, final Class<T> responseClass) {
Type listType = new TypeToken<List<responseClass>>(){}.getType();
JSONArray jsonResponse = new JSONObject(resourceResponse).getJSONArray(jsonObject);
return gson.fromJson(jsonResponse, listType);
}
And calling it like this:
getResponseObjectAsArray(resourceResponse, "identityList", Identity.class)
With resourceResponse
being the json response in a string format. But I get a syntax error with the getResponseObjectAsArray
method that says:
Error:(39, 44) java: cannot find symbol
symbol: class responseClass
I'm trying to parametrize the list with whatever class passed into the method because it could be a list of many other types and not just Identity
. What am i doing wrong here?
Edit 1: Tried the solution of List<T>
and got this error now:
Error:(41, 20) java: no suitable method found for fromJson(org.json.JSONArray,java.lang.reflect.Type)
method com.google.gson.Gson.<T>fromJson(java.lang.String,java.lang.Class<T>) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; org.json.JSONArray cannot be converted to java.lang.String))
method com.google.gson.Gson.<T>fromJson(java.lang.String,java.lang.reflect.Type) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; org.json.JSONArray cannot be converted to java.lang.String))
method com.google.gson.Gson.<T>fromJson(java.io.Reader,java.lang.Class<T>) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; org.json.JSONArray cannot be converted to java.io.Reader))
method com.google.gson.Gson.<T>fromJson(java.io.Reader,java.lang.reflect.Type) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; org.json.JSONArray cannot be converted to java.io.Reader))
method com.google.gson.Gson.<T>fromJson(com.google.gson.stream.JsonReader,java.lang.reflect.Type) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; org.json.JSONArray cannot be converted to com.google.gson.stream.JsonReader))
method com.google.gson.Gson.<T>fromJson(com.google.gson.JsonElement,java.lang.Class<T>) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; org.json.JSONArray cannot be converted to com.google.gson.JsonElement))
method com.google.gson.Gson.<T>fromJson(com.google.gson.JsonElement,java.lang.reflect.Type) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; org.json.JSONArray cannot be converted to com.google.gson.JsonElement))
Edit 2: Here's another attempt at parametrizing a list object:
public static <T> List<T> getResponseObjectAsArray(String resourceResponse, String jsonObject, Class<T> responseClass) {
JSONArray jsonResponse = new JSONObject(resourceResponse).getJSONObject("identityList").getJSONArray(jsonObject);
return gson.fromJson(jsonResponse.toString(), List<responseClass>);
}
I just hardcoded the identityList string for now but later I'll make it as user input. But I'm still unable to parametrize the list inside of the fromJson
call. I'm getting an Expression expected
error.
>(){}.getType(); where is the responseClass defined? Either use a wildcard or exact class definition
– Shriram Feb 08 '16 at 16:41