3

I'm trying to simply get values from my Gson Object, so in my "city" Object:

LinkedTreeMap Array

I need to get the name "first" and the boolean value from "isSelected".

I just wrote a new class "KeyPairBoolData":

class KeyPairBoolData {
    int id;
    String name;
    boolean isSelected;

}

but I have no idea what should I write in the new class to get these values, here is how I'm trying to get it:

List<KeyPairBoolData> city = gson.fromJson(myListArray, listArray.getClass());

any idea? Thanks!

Community
  • 1
  • 1
Eran Levi
  • 363
  • 7
  • 22

1 Answers1

4

Try this:

Method to deserialize generic collection:

Type listType = new TypeToken<ArrayList<KeyPairBoolData>>() {
                    }.getType();
List<KeyPairBoolData> keyPairBoolDataList = new Gson().fromJson(jsonArray, listType);

Import : import java.lang.reflect.Type;

Answer from https://stackoverflow.com/a/5554296/4848308

Community
  • 1
  • 1
Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57