JSON 到对象(Gson 库)

让我们假设你有一个名为 Person 的类,只有 name

private class Person {
    public String name;

    public Person(String name) {
        this.name = name;
    }
}

码:

Gson gson = new Gson();
String json = "{\"name\": \"John\"}";

Person person = gson.fromJson(json, Person.class);
System.out.println(person.name); //John

你必须在类路径中包含 gson 库