问题
i have class like this
public class Response<T>{
T item;
List<T>;
}
so how can to deserializeding String in this pattern to one generic class withgson.fromJson(targetString,...)
?
i glad to used in retrofit library where Response
is returned parameter in desired interface
回答1:
convert list to gson
public <T> String setList(List<T> list) {
Gson gson = new Gson();
return gson.toJson(list);
}
convert gson to list
public List<(desired class)> getList(){
Gson gson = new Gson();
String json = (pass json string);
Type type = new TypeToken<List<(desired class)>>() {}.getType();
return gson.fromJson(json, type);
}
来源:https://stackoverflow.com/questions/54273984/converting-json-string-with-gson-for-generic-model