将数据转成json内容

//关键
首先将json字符串转换为json对象,然后再解析json对象,过程如下

String s = JSON.toJSONString(person);

案例:

学生列:

public class Student {
    private String name;
    private int age;
    private String phone;

    public Student() {
    }

    public Student(String name, int age, String phone) {
        this.name = name;
        this.age = age;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}
public class SchoolClass {
    private ClassMsg cm;
    private String app;
    List<Student> students;

    public SchoolClass() {
    }

    public SchoolClass(ClassMsg cm, String app, List<Student> students) {
        this.cm = cm;
        this.app = app;
        this.students = students;
    }

    public ClassMsg getCm() {
        return cm;
    }

    public void setCm(ClassMsg cm) {
        this.cm = cm;
    }

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }
public class Person {
    private int age;
    private String name;
    private Date birthday;

    public Person() {
    }

    public Person(int age, String name, Date birthday) {
        this.age = age;
        this.name = name;
        this.birthday = birthday;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
public class ClassMsg {
    private int num; //班级人数
    private Date startDate; //开班日期
    private String leader;
    private String teacher;

    public ClassMsg() {
    }

    public ClassMsg(int num, Date startDate, String leader, String teacher) {
        this.num = num;
        this.startDate = startDate;
        this.leader = leader;
        this.teacher = teacher;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public String getLeader() {
        return leader;
    }

    public void setLeader(String leader) {
        this.leader = leader;
    }

    public String getTeacher() {
        return teacher;
    }

    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }
}

测试类:

public class FastJsonDemo {
    public static void main(String[] args) {
        Person person = new Person();
        person.setAge(36);
        person.setName("张三");
        person.setBirthday(new Date());

        Person person2 = new Person();
        person2.setAge(26);
        person2.setName("李四");
        person2.setBirthday(new Date());

        //转换
        String s = JSON.toJSONString(person);
        System.out.println(s);

        ArrayList list = new ArrayList<>();
        list.add(person);
        list.add(person2);

        String s1 = JSON.toJSONString(list);
        System.out.println(s1);

        SchoolClass sc = new SchoolClass();
        ClassMsg cm = new ClassMsg();
        ArrayList stuInfo = new ArrayList<>();
        stuInfo.add(new Student("a",21,"21235545"));
        stuInfo.add(new Student("b",25,"61551515"));
        stuInfo.add(new Student("c",28,"16815154"));

        cm.setLeader("rw");
        cm.setNum(25);
        cm.setStartDate(new Date());
        cm.setTeacher("lx");

        sc.setCm(cm);
        sc.setApp("fastJson");
        sc.setStudents(stuInfo);

        System.out.println(JSON.toJSONString(sc));
    }
}

结果:

{"age":36,"birthday":1620908540868,"name":"张三"}
[{"age":36,"birthday":1620908540868,"name":"张三"},{"age":26,"birthday":1620908540868,"name":"李四"}]
{"app":"fastJson","cm":{"leader":"rw","num":25,"startDate":1620908541236,"teacher":"lx"},"students":[{"age":21,"name":"a","phone":"21235545"},{"age":25,"name":"b","phone":"61551515"},{"age":28,"name":"c","phone":"16815154"}]}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值