Parsing a ListString with Gson

suggest change

Method 1

Gson gson = new Gson();
String json = "[ \"Adam\", \"John\", \"Mary\" ]";

Type type = new TypeToken<List<String>>(){}.getType();
List<String> members = gson.fromJson(json, type);
Log.v("Members", members.toString());

This is useful for most generic container classes, since you can’t get the class of a parameterized type (ie: you can’t call List<String>.class).

Method 2

public class StringList extends ArrayList<String> { }


List<String> members = gson.fromJson(json, StringList.class);

Alternatively, you can always subclass the type you want, and then pass in that class. However this isn’t always best practice, since it will return to you an object of type StringList;

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents