字符串数组存储在al arraylist中,它是JSON格式。现在我想将该arraylist转换为普通对象。我如何转换它?
public class Json {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add(1);
al.add("Lokesh");
al.add("Gupta");
al.add(Arrays.asList("ADMIN", "MANAGER"));
Gson gson = new Gson();
System.out.println(gson.toJson(al));
}}
答案 0 :(得分:0)
使用您的属性创建一个对象, 例如: -
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);
}
}