Android 中使用Gson进行list集合的序列化与反序列化
Posted 路宇_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 中使用Gson进行list集合的序列化与反序列化相关的知识,希望对你有一定的参考价值。
重点:
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());
以上是关于Android 中使用Gson进行list集合的序列化与反序列化的主要内容,如果未能解决你的问题,请参考以下文章
Gson 在单个 JSON 对象中使用两种 List 类型进行序列化
如何使用 Gson 将 JSONArray 转换为 List?