public static void main(String[] args) {
String user = "{username:li,password:123456,sex:man}";
Gson gson = new Gson();
User result = gson.fromJson(user, User.class);
System.out.println("username:" + result.getUsername() + " password:" + result.getPassword() + " sex:" + result.getSex());
User us = new User();
us.setUsername("LI");
us.setPassword("123456");
us.setSex("man");
Gson gson2 = new Gson();
String reString = gson2.toJson(us);
System.out.println(reString);
// Gson gson = new GsonBuilder().create();
String userList = "[{username:li,password:123456,sex:man},{username:wang,password:1111,sex:woman}]";
Gson gson3 = new Gson();
List<User> list = gson3.fromJson(userList, new TypeToken<ArrayList<User>>() {
}.getType());
System.out.println(list.get(0).getUsername() + " " + list.get(0).getPassword() + " " + list.get(0).getSex());
System.out.println(list.get(1).getUsername() + " " + list.get(1).getPassword() + " " + list.get(1).getSex());
}
=======================================================================================
username:li password:123456 sex:man
{"username":"LI","password":"123456","sex":"man"}
li 123456 man
wang 1111 woman