Skip to content Skip to sidebar Skip to footer

Converting Json String With Gson For Generic Model

i have class like this public class Response{ T item; List; } so how can to deserializeding String in this pattern to one generic class withgson.fromJson(t

Solution 1:

convert list to gson

public <T> StringsetList(List<T> list) {
    Gson gson = newGson();
    return gson.toJson(list);  
}

convert gson to list

publicList<(desired class)> getList(){
    Gson gson = newGson();
    String json = (pass json string);
    Typetype = newTypeToken<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'

Post a Comment for "Converting Json String With Gson For Generic Model"