public static void main(String[] args) {
Gson gson = new Gson(); List<Person> persons = new ArrayList<Person>(); for (int i = 0; i < 10; i++) { Person p = new Person(); p.setName("name" + i); p.setAge(i * 5); persons.add(p); } String str = gson.toJson(persons); System.out.println(str); Type type = new TypeToken<List>() { }.getType(); List list = gson.fromJson(str, type); for (Iterator it = list.iterator(); it.hasNext();) { System.out.println(it.next()); } }static class Person {
private String name;
private int age;/**
* @return the name */ public String getName() { return name; }/**
* @param name the name to set */ public void setName(String name) { this.name = name; }/**
* @return the age */ public int getAge() { return age; }/**
* @param age the age to set */ public void setAge(int age) { this.age = age; }@Override
public String toString() { return name + ":" + age; } }