gson Getting started with gson Arrays


Example

JSON:

[
  {
    "id": 8484,
    "name": "David",
    "height": 173.2,
    "weight": 75.42
  },
  {
    "id": 8485,
    "name": "Ronald",
    "height": 183.73,
    "weight": 83.1
  }
]

Person.java

public class Person {
    public int id;
    public String name;
    public double height;
    public double weight;

    @Override
    public String toString() {
        return "[ id: " + String.valueOf(id) + ", name: " + name + ", height: " + String.valueOf(height) + ", weight: " + String.valueOf(weight) + " ]";
    }
}

Usage:

Gson gson = new Gson();
Person[] persons = gson.fromJson(json, Person[].class);
for(Person person : persons)
    System.out.println(person.toString());

Output:

[ id: 8484, name: David, height: 173.2, weight: 75.42 ]
[ id: 8485, name: Ronald, height: 183.73, weight: 83.1 ]