更新时间:2023-02-15 18:16:46
使用属性创建对象, 例如:-
Create an Object with your attributes, eg:-
public class User {
Integer id;
String firstName,
String lastName;
List<Role> roles;// Where roles is a list of another Object Role
//getter and setter methods
}
然后
public class Manager{
public static void main(String[] args) {
String json = "{'id':1,'firstName':'Deva','lastName':'Raj','roles':['ADMIN','MANAGER']}";
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
System.out.println(user);
}
}