Gson解析數組array和list容器


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM