Android Gson Parsing a List with Gson

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


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;

Got any Android Question?