gson解析json串 发表于 2019-07-06 | 阅读次数: | 本文字数: 1.2k | 阅读时长 ≈ 1 分钟 先定义一个Java对象12345678910111213141516public class Student { public String name; public int age; public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; }} json转Java对象123456public void toObject() { // {"age":28,"name":"野猿新一"} String json = "{\"age\":28,\"name\":\"野猿新一\"}"; Student student = new Gson().fromJson(json, Student.class); Log.d("gson", student.toString());} json转List12345678public void toList() { // [{"age":21,"name":"野猿新一"},{"age":22,"name":"野猿新二"},{"age":23,"name":"野猿新三"}] String json = "[{\"age\":21,\"name\":\"野猿新一\"},{\"age\":22,\"name\":\"野猿新二\"},{\"age\":23,\"name\":\"野猿新三\"}]"; List<Student> students = new Gson().fromJson(json, new TypeToken<List<Student>>(){}.getType()); for (Student student:students) { Log.d("gson", student.toString()); }}