0

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

FxRi4
  • 1,096
  • 10
  • 15

1 Answers1

0

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);
}

Add gson dependency to the app gradle file.

implementation 'com.google.code.gson:gson:2.8.6'