使用Gson解析首先需要加入架包文件:gson-2.2.4.jar
定义一个类Student:
public class Student {
String name="xiao";
String sex="男";
}
定义Java文件:
public class ListToGson {
public static void main(String[] args) {
Student[] people= new Student[]{new Student(),new Student(),new Student()};
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student());
list.add(new Student());
list.add(new Student());
String json = new Gson().toJson(list);
String peoples = new Gson().toJson(people);
System.out.println(json);
System.out.println(peoples);
}
}
运行结果如下:
对list解析:[{"name":"xiao","sex":"男"},{"name":"xiao","sex":"男"},{"name":"xiao","sex":"男"}]
对数组解析:[{"name":"xiao","sex":"男"},{"name":"xiao","sex":"男"},{"name":"xiao","sex":"男"}]
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-07 12:24:30