package com.qf.mobiletrain01; import java.util.List; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; class Student { private int age; private String name; private double money;// 余额 public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public void setMoney(double money) { this.money = money; } @Override public String toString() { return "Student [age=" + age + ", name=" + name + ", money=" + money + "]"; } } //注意:需导入包import com.google.gson.Gson; public class TestGson { public static void main(String[] args) { String json = "{name:'李敏杰',age:250,money:-10000}"; // 创建Gson对象 Gson gson = new Gson(); // Class<Student> c= Student.class; System.out.println(gson.fromJson(json, Student.class)); String jsons = "[{name:'李敏杰',age:250,money:-10000},{name:'杨杰',age:500,money:10000}]"; // json-->集合 // TypeToken 类型标识器,gson无需设置get()set()方法 TypeToken<List<Student>> token = new TypeToken<List<Student>>() { }; List<Student> list = gson.fromJson(jsons, token.getType()); System.out.println(list); } }