工作内容:
1.如何Gson解析JSON字符串讲解。
2.LruCache缓存机制的讲解。
3.GsonFormat一键生成对应Json字符串的数据对象
学习分享:
一、Gson解析JSON字符串
1.需明确Gson解析JSON字符串与使用JSONObject,JSONArray解析JSON字符串一样,需要逐层解析。
2.Gson的优势在于,可以轻松将JSON字符串中的数据转换成实际类(自定义类,属性名与JSON对象的键值同名)
注意:为减少中间类,可以在解析到想要数据之前的一级开始使用中间类,最后通过这个类的属性获取到你想要的“对象”或者“对象数组/List等”
3.Gson创建JSON字符串
4.GsonFormat一键生成对应Json字符串的数据对象(1.下载插件:Android studio中file → settings → Plugins → Browse respositories → 输入GsonFormat) 使用:创建一个类,在类体中使用快捷键“Alt+Insert” → "GsonFormat" → "放入JSON字符串(完整的一个JSON字符串)" → OK
下面2个例子:
例子1:解析一个身份识别的字符串(分别用JSON,Gson,GsonBuilder.create一个Gson对象)
String jsonString = "
{"data":{"address":"天津市静海县","birthday":"1990-07-22","constellation":"巨蟹座","gender":"男","idcard":"120223199007220174","zodiac":"马"},"error":0,"msg":"succeed"}"
//身份识别 public static String getPersonalInfo(String urlString) { PersonInfo personInfo = null; StringBuilder info = new StringBuilder(); String jsonString = JSONUtil.request(urlString); //JSON方式解析JSON字符串 // try { // JSONObject jsonObject = new JSONObject(jsonString); // if ("succeed".equals(jsonObject.getString("msg"))) { // Log.e("**身份识别**", "获取到数据"); // JSONObject object = jsonObject.getJSONObject("data"); // String birthday = object.getString("birthday"); // String address = object.getString("address"); // String sex = object.getString("gender"); // String idcard = object.getString("idcard"); // String zodiac = object.getString("zodiac"); // personInfo = new PersonInfo(birthday,address,sex,idcard,zodiac); //Gson方式一 Gson gson1 = new Gson(); //Gson方式二(GsonBuilder——灵活性强) GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setDateFormat(DateFormat.LONG) //时间信息设置 // .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) //设置字段(即Key值)首字母大写——暂时没用 // .setPrettyPrinting() //对JSON结果格式化,添加换行等——生成JSON数据的时候用 // .setVersion(1.0) //设置版本号——暂时没用 .create(); Gson gson2 = gsonBuilder.create();//必须使用create才能创建成功 //这里使用了gson2.gson1使用也可以达到同样的效果 PersonInfoGsonObj personInfoGsonObj = gson2.fromJson(jsonString, PersonInfoGsonObj.class); if("succeed".equals(personInfoGsonObj.getMsg()))//判断获取成功 personInfo = personInfoGsonObj.getData(); info.append("性别:"+personInfo.getGender()+"\r\n"); info.append("生肖:"+personInfo.getZodiac()+"\r\n"); info.append("生日:"+personInfo.getBirthday()+"\r\n"); info.append("地址:"+personInfo.getAddress()+"\r\n"); info.append("身份证号码:"+personInfo.getIdcard()); // } // } catch (JSONException e) { // e.printStackTrace(); // } return info.toString(); }涉及的中间类:
public class PersonInfoGsonObj { /** * 最后要获得的对象(data是JSON中的键) * 如果JSON字符串中 “data”:[{对象1},{对象2},{...}...{对象N}] //对象都是类A的对象 * 则可以即将data的属性设置为:public List data; * 在通过下列步骤: * PersonInfoGsonObj obj = gson.from(JSONString,PersonInfoGsonObj.class) * List aList = obj.data * 就可以轻松获得A类对象的List */ private PersonInfo data; private String msg; //msg用于判断是否连接成功(data也是JSON中的键,对应值是Succes表示获取成功) public PersonInfoGsonObj(PersonInfo data, String msg) { this.data = data; this.msg = msg; } public PersonInfo getData() { return data; } public String getMsg() { return msg; } }最终要得到的类:
public class PersonInfo { private String birthday; private String address; private String gender;//性别 private String idcard;//身份证号码 private String zodiac;//生肖 public String getBirthday() { return birthday; } public String getAddress() { return address; } public String getGender() { return gender; } public String getIdcard() { return idcard; } public String getZodiac() { return zodiac; } public PersonInfo(String birthday, String address, String gender, String idcard, String zodiac) { this.birthday = birthday; this.address = address; this.gender = gender; this.idcard = idcard; this.zodiac = zodiac; } }例子 2 :生成JSON数据(使用上面的中间类和里层类)
//测试生成JSON数据 Gson gson = new GsonBuilder() // .enableComplexMapKeySerialization() //设置反序列化格式 // .serializeNulls() //设置列化格式 // .setDateFormat(DateFormat.LONG)//设置时间格式 .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//会把字段首字母大写 .setPrettyPrinting() //对JSON结果格式化,添加换行等——生成JSON数据的时候用 // .setVersion(1.0) //设置版本号——暂时没用 .create(); //必须使用create才能创建成功 PersonInfo personInfo1 = new PersonInfo("12-9","京东路天猫店","女","123456","110203478559895"); String json1 = gson.toJson(personInfo1); Log.e("生成json数据",json1);
生成json数据: {
"Address": "京东路天猫店",
"Birthday": "12-9",
"Gender": "女",
"Idcard": "123456",
"Zodiac": "110203478559895"
}
下面是生成:外层对象,对象内含对象数组
List生成json数据: {"personinfo":[{"Birthday":"12-9","Idcard":"123456","Zodiac":"110203478559895","Gender":"女","Address":"京东路天猫店"},{"Birthday":"12-9","Idcard":"123456","Zodiac":"110203478559895","Gender":"女","Address":"京东路天猫店"}]}personInfos = new ArrayList<>(); personInfos.add(new PersonInfo("12-9","京东路天猫店","女","123456","110203478559895")); personInfos.add(new PersonInfo("12-9","京东路天猫店","女","123456","110203478559895")); personInfos.add(new PersonInfo("12-9","京东路天猫店","女","123456","110203478559895")); personInfos.add(new PersonInfo("12-9","京东路天猫店","女","123456","110203478559895")); personInfos.add(new PersonInfo("12-9","京东路天猫店","女","123456","110203478559895")); personInfos.add(new PersonInfo("12-9","京东路天猫店","女","123456","110203478559895")); String json2 = gson.toJson(personInfos);//这里得到的是:[{PersonInfo},{PersonInfo}...] String json3=""; try { JSONArray array = new JSONArray(json2);//将内层对象数组转换成JSONArray JSONObject object = new JSONObject();//生成外层对象 object.put("personinfo",array);//将内层对象数组设置成外层对象的属性 json3 = object.toString(); } catch (JSONException e) { e.printStackTrace(); } Log.e("生成json数据",json3);