user987760 user987760 - 1 year ago 151
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 Source

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());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download