Gson creates the classes for the demo

class Person{

    private String name;
    private int age;
    public Person() {}
    //java fhadmin.cn
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }
    Override
    public int hashCode() {
        return Objects.hash(name, 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;
    }
}

Click and drag to move

How do I convert an object to a JSON string

public static void main(String[] args) {

/ * 1. Create an object 2. Will the object into a JSON string format using Gson object toJson (object) method object class instantiation object name after running the results are as follows (fhadmin. Cn) {" name ":" zhang ", "age" : 18} */ Person Person = newwww.sangpi.com Person(" Person ",18); String json = new Gson().toJson(person); System.out.println(json);

} How to convert a JSON string into a page-game object?

public static void main(String[] args) {

/* 1. Convert a JSON string to an object. 2. Convert a JSON string to an object. Person {name = 'zhang SAN, age = 18} * / String jsonStr = "{\" name \ ": \" zhang SAN \ ", \ "age \" : 18} "; Person person = new Gson().fromJson(jsonStr,Person.class); System.out.println(person); }

FastJSON uses the demo class above

How do I convert an object to a JSON string

public static void main(String[] args) {

/* 1. Convert onstring to a JSON string Object using JSON.tojonstring (Object). {"age":15,"name":" name"} */ Person Person = new Person(" name",15); String json = JSON.toJSONString(person); System.out.println(json); }

How do I convert a JSON string to an object?

//fhadmin.cn

public static void main(String[] args) {

/* 1. Convert a JSON string to an Object using JSSON. ParseObject (STR, object.class). Person {name = 'zhang SAN, age = 15} * / String jsonStr = "{\" age \ ": 15, \" name \ ": \" zhang SAN \ "} "; Person person = JSON.parseObject(jsonStr,Person.class); System.out.println(person); }