重点:
Type type =new Typetoken<List<Student>>(){
}.getType();
把type对象直接传入到fromJson中
List<Student> list = new ArrayList<>();
list.add(new Student("小张","男",20,"读书"));
list.add(new Student("小明","男",20,"跑步"));
list.add(new Student("小红","女",20,"旅游"));
list.add(new Student("小白","男",20,"唱歌"));
//将list集合序列化
String s = new Gson().toJson(list);
System.out.println("序列化为:"+s);
//将list集合反序列化
Type type =new Typetoken<List<Student>>(){
}.getType();
List<Student> list1 = new Gson().fromJson(s,type);
System.out.println(list1.get(1).getName());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。