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 庫