欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java对【JSON数据的解析】--Gson解析法

程序员文章站 2024-03-16 08:43:10
...

Gson和fastjson分别为谷歌和阿里巴巴对JSON数据进行处理封装的jar包


两者异同点:

相同点:都是根据JSON数据创建相应的类

不同点:
    1.调用方式区别
    谷歌:方法都是非静态的,需要先创建对象,再去调用
    阿里:方法都是静态的,直接用类调用,不需要创建对象

    2.方法区别:
    谷歌:fromJson(参数1,参数2)
    阿里:JSON.parseXX(参数1,参数2)
        具体是parseObject()还是parseArray(),根据需要定

要求:解析下面JSON数据

1.String string = “{name:’zhangsan’,age:18}”;

2.String string = “{persons:[{name:’zhangsan’,age:20},{name:’lisi’,age:21},{name:’wangwu’,age:22}]}”;

3.String string = “{no:1,name:’android’,employees:[{name:’zhangsan’,age:20},{name:’lisi’,age:21},{name:’wangwu’,age:22}]}”;


代码:


第一题:

package 谷歌和阿里解析JSON数据;

import com.google.gson.Gson;

public class Test {
    public static void main(String[] args) {
        //Gson解析JSON数据
        //1.创建对象Person,有属性:name,age--创建结束
        String string = "{name:'zhangsan',age:18}";

        Gson gson=new Gson();
        Person person=gson.fromJson(string, Person.class);
        System.out.println(person.getName()+","+person.getAge());
    }
}

class Person{
    private String name;
    private int age;
    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;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Person() {
        super();
    }

}

运行结果:

Java对【JSON数据的解析】--Gson解析法


第2题:

package 谷歌和阿里解析JSON数据;

import java.util.ArrayList;

import com.google.gson.Gson;

public class Test2 {
    public static void main(String[] args) {
        //Gson解析时,遇到对象创建对象,这个对象里所有的内容键都作为对象的属性
        //1.创建第一个对象,有属性:persons,且为集合
        //2.persons集合里是对象,设对象为Person
        //3.Person,它有两个属性:name,age
        //4.相应的类创建完毕后,就可以解析了
        String string = "{persons:[{name:'zhangsan',age:20},{name:'lisi',age:21},{name:'wangwu',age:22}]}";

        Gson gson=new Gson();
        Persons persons=gson.fromJson(string, Persons.class);
        System.out.println(persons);
    }
}

class Persons{
    private ArrayList<Person1> persons;

    public ArrayList<Person1> getPersons() {
        return persons;
    }

    public void setPersons(ArrayList<Person1> persons) {
        this.persons = persons;
    }


    @Override
    public String toString() {
        return "Persons [persons=" + persons + "]";
    }

    public Persons(ArrayList<Person1> persons) {
        super();
        this.persons = persons;
    }

    public Persons() {
        super();
    }

}

class Person1{
    private String name;
    private int age;
    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;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    public Person1(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Person1() {
        super();
    }
}

运行结果
Java对【JSON数据的解析】--Gson解析法


第三题:

package 谷歌和阿里解析JSON数据;

import java.util.ArrayList;

import com.google.gson.Gson;

public class Test3 {
    public static void main(String[] args) {
        //1.第一个对象有属性:no,name,employees,且employees为集合
        //2.employees数组中有对象,设该对象为Person
        //3.Person有属性:name,age----创建结束
        String string = "{no:1,name:'android',employees:[{name:'zhangsan',age:20},{name:'lisi',age:21},{name:'wangwu',age:22}]}";

        Gson gson=new Gson();
        Demo demo=gson.fromJson(string, Demo.class);
        System.out.println(demo);
    }
}

class Demo{
    private int no;
    private String name;
    private ArrayList<Person2> employees;
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public ArrayList<Person2> getEmployees() {
        return employees;
    }
    public void setEmployees(ArrayList<Person2> employees) {
        this.employees = employees;
    }
    @Override
    public String toString() {
        return "Demo [no=" + no + ", name=" + name + ", employees=" + employees + "]";
    }
    public Demo(int no, String name, ArrayList<Person2> employees) {
        super();
        this.no = no;
        this.name = name;
        this.employees = employees;
    }
    public Demo() {
        super();
    }

}

class Person2{
    private String name;
    private int age;
    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;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    public Person2(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Person2() {
        super();
    }
}

运行结果:

Java对【JSON数据的解析】--Gson解析法

Java对【JSON数据的解析】--Gson解析法