码迷,mamicode.com
首页 > 编程语言 > 详细

Gson解析数组array和list容器

时间:2017-07-26 18:09:17      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:ring   csdn   print   ace   解析   str   ase   href   tle   

http://blog.csdn.net/lucky_bo/article/details/47657641

Gson解析数组和list容器

 

使用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":"男"}]

Gson解析数组array和list容器

标签:ring   csdn   print   ace   解析   str   ase   href   tle   

原文地址:http://www.cnblogs.com/robbinluobo/p/7240029.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!