2.2 字符串jsonData如下,图为运行结果
[{ "stu_no":12345,"stu_name":"John","stu_sex":"male"
},{ "stu_no":12346,"stu_name":"Tom","stu_sex":"male"
},{"stu_no":12347,"stu_name":"Lily","stu_sex":"female"}]
3.GSON的使用方法
3.1 下载并安装
•将下载的gson-2.6.1.jar复制到 项目目录->app->libs 文件夹下
3.2 方法简介
•toJson(params1),将传入对象转换为字符串
•fromJson(params1,params2),传入两个参数,将字符串params1转换为params2指定的数据类型。
3.3 示例代码
3.3.1 单个对象的解析
public class Student {
private int stu_no;
private String stu_name;
private String stu_sex;
Student(int stu_no,String stu_name,String stu_sex){
this.stu_no = stu_no;
this.stu_name = stu_name;
this.stu_sex = stu_sex;
}
}
// 序列化,将Student对象stu转换为字符串str
Student stu = new Student(123,"Tom","male");
Gson gson = new Gson();
String str = gson.toJson(stu);
//反序列化,将字符串转换为Student对象
jsonData = "{ "stu_no":12345,"stu_name":"John","stu_sex":"male" }";
Gson gson = new Gson();
Student student = gson.fromJson(jsonData,Student.class);












