使用Gson解析json数据

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值