使用Gson解析json数据

来源:互联网 发布:乐清知临学校学费价格 编辑:程序博客网 时间:2024/04/27 22:12

Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。
首先我们需要定义一个序列化的Bean,这里采用内部类的形式,看起来会比较清晰一些:

package com.example.gsondemo;import java.util.Date;import java.util.List;public class Class {    private int num;    private List<Student> students;    public Class(int num, List<Student> students) {        // TODO Auto-generated constructor stub        this.num = num;        this.students = students;    }    public int getNum() {        return num;    }    public void setNum(int num) {        this.num = num;    }    public List<Student> getStudents() {        return students;    }    public void setStudents(List<Student> students) {        this.students = students;    }    public static class Student {        private String name;        private String address;        private Date dateOfBirth;        private Family family;        public Student(String name, String address, Date dateOfBirth, Family family) {            this.name = name;            this.address = address;            this.dateOfBirth = dateOfBirth;            this.family = family;        }        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }        public String getAddress() {            return address;        }        public void setAddress(String address) {            this.address = address;        }        public Date getDateOfBirth() {            return dateOfBirth;        }        public void setDateOfBirth(Date dateOfBirth) {            this.dateOfBirth = dateOfBirth;        }        public Family getFamily() {            return family;        }        public void setFamily(Family family) {            this.family = family;        }        public static class Family {            private String father;            private String mother;            public Family(String father, String mother) {                this.father = father;                this.mother = mother;            }            public String getFather() {                return father;            }            public void setFather(String father) {                this.father = father;            }            public String getMother() {                return mother;            }            public void setMother(String mother) {                this.mother = mother;            }        }    }}

对应的JSON数据:

{    "num": 30,    "students": [        {            "address": "Apple St",            "dateOfBirth": "Nov 1, 3900 00:00:00",            "family": {                "father": "aa",                "mother": "bb"            },            "name": "Alice"        },        {            "address": "Banana St",            "family": {                "father": "aa",                "mother": "bb"            },            "name": "Bob"        },        {            "address": "Grape St",            "dateOfBirth": "Jun 21, 3900 00:00:00",            "family": {                "father": "cc",                "mother": "dd"            },            "name": "Carol"        },        {            "address": "Mango St",            "family": {                "father": "cc",                "mother": "dd"            },            "name": "Mallory"        }    ]}

生成和解析json:

        Family family = new Family("aa", "bb");        Student a = new Student("Alice", "Apple St", new Date(2000, 10, 1), family);        Student b = new Student("Bob", "Banana St", null, family);        family = new Student.Family("cc", "dd");        Student c = new Student("Carol", "Grape St", new Date(2000, 5, 21), family);        Student d = new Student("Mallory", "Mango St", null, family);        List<Student> students = new ArrayList<Student>();        students.add(a);        students.add(b);        students.add(c);        students.add(d);        Class classString = new Class(30, students);        Gson gson = new Gson();        String classjson = gson.toJson(classString);        System.out.println("classjson=" + classjson);        Class classObject = gson.fromJson(classjson, Class.class);        for (Student student : classObject.getStudents()) {            System.out.println("studentname=" + student.getName());        }
0 0
原创粉丝点击