/** * @author lautturi.com * Java example: parse json string and convert jsonarray to list of object in java */ import java.util.*; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; class Employee{ public Employee(String string, int i) { name = string; age = i; } public String name; public int age; } public class Lautturi { public static void main(String[] args) { String jsonString = "[{\"age\":22,\"name\":\"Alice\"},{\"age\":25,\"name\":\"Lautturi\"}]"; Gson gson = new Gson(); Type type = new TypeToken<List<Employee>>(){}.getType(); List<Employee> empList = gson.fromJson(jsonString, type); for (Employee emp : empList){ System.out.println("Employe Details: "+ emp.age + "-" + emp.name); } } }
output:
Employe Details22-Alice Employe Details25-Lautturi