user987760 user987760 - 1 month ago 5
JSON Question

Gson, parse Json Array without wrapper

I have this response:

[{ "age" : "12",
"name" : "name1"

},
{ "age" : "21",
"name" : "name2"

}]


How can i parse this response using Gson

ArrayList<PersonModel> persons = gson.fromJson(response, PersonModel.class);


this will not work with me

Answer

Since you're trying to convert your JSON array to an ArrayList of PersonModel (I'm assuming you already figured out how to write the PersonModel.class), you should use TypeToken to convert the JSON response to ArrayList.

ArrayList<PersonModel> persons = gson.fromJson(response, new TypeToken<List<PersonModel>>(){}.getType());
Comments