【发布时间】:2016-02-08 16:36:16
【问题描述】:
我正在尝试将 json 数组响应存储到数组列表中。 JSON 响应如下所示:
"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",
}
]
}
这是Identity 类:
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;
}
}
根据我在 here 等其他 SO 帖子上发现的内容,我编写了这个函数来解析它:
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);
}
然后这样称呼它:
getResponseObjectAsArray(resourceResponse, "identityList", Identity.class)
resourceResponse 是字符串格式的 json 响应。但是getResponseObjectAsArray 方法出现语法错误:
Error:(39, 44) java: cannot find symbol
symbol: class responseClass
我正在尝试使用传递给方法的任何类对列表进行参数化,因为它可能是许多其他类型的列表,而不仅仅是Identity。我在这里做错了什么?
编辑1:尝试了List<T>的解决方案,现在得到了这个错误:
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))
编辑 2:这是另一个参数化列表对象的尝试:
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>);
}
我现在只是硬编码了 identityList 字符串,但稍后我会将其作为用户输入。但我仍然无法对fromJson 调用中的列表进行参数化。我收到 Expression expected 错误。
【问题讨论】:
-
TypeToken
- >(){}.getType(); responseClass 在哪里定义?使用通配符或确切的类定义
标签: java arrays json list gson