Json转换利器Gson之-实际开发中的特殊需求处理(手动解析-TypeToken处理)

【Json转换利器Gson之-实际开发中的特殊需求处理(手动解析-TypeToken处理)】下面是开发中遇到的复杂JSON,用TypeToken来进行处理:(当然还有第二种解析方式,请查看http://blog.csdn.net/jason_996/article/details/78039799)
处理方法:
1,先将接口返回的JSON,用gson.tojson(map)方法,将LinkedTreeMap对象转成String。
2,再通过gson.fromjson(string,type)方法,将String转成LinkedTreeMap。用到了Type type=new TypeToken(){}.getType;
3,再将LinkedTreeMapf进行遍历
下面直接上代码:(例子中的数据内容与demo不一样,但数据 结构是一样的,为了不让公司的信息你懂得)

private String jsonString = "{\"Members\":{\"Owner\":{\"ID\":\"26\",\"LoginName\":\"gm1\",\"Name\":\"总经理A\",\"Email\":\"uwa@qq.com\"},\"Host\":{\"ID\":\"29\",\"LoginName\":\"gm3\",\"Name\":\"总经理C\",\"Email\":\"\"},\"Recorder\":{\"ID\":\"26\",\"LoginName\":\"gm1\",\"Name\":\"总经理A\",\"Email\":\"aa@qq.com\"},\"Members\":[{\"ID\":\"26\",\"LoginName\":\"gm1\",\"Name\":\"总经理A\",\"Email\":\"wa@qq.com\"},{\"ID\":\"28\",\"LoginName\":\"gm2\",\"Name\":\"总经理B\",\"Email\":\"zae1s@qq.com\"},{\"ID\":\"29\",\"LoginName\":\"gm3\",\"Name\":\"总经理C\",\"Email\":\"\"}],\"Attendance\":[{\"ID\":\"29\",\"LoginName\":\"gm3\",\"Name\":\"总经理C\",\"Email\":\"\"}],\"Absence\":[{\"LoginName\":\"gm1\",\"Name\":\"总经理A\",\"Email\":\"uw30@qq.com\"},{\"LoginName\":\"gm2\",\"Name\":\"总经理B\",\"Email\":\"zdps5@qq.com\"}]},\"Room\":{\"MeetingID\":\"6fa7eef5-6735-4fb1-a992-8088250be948\",\"MeetingRoomID\":\"2ebbe73b-bee4-4a9e-81c9-f8ffc6ca1149\",\"Address\":\"金融港-302会议室\",\"Date\":\"2017-09-14\",\"Text\":\"2017-09-14 14:00-15:00\",\"Value\":\"[14:00][14:30]\"},\"Attachments\":{\"Items\":[{\"Actor\":\"总经理A\",\"File\":\"1503808491.zip\",\"Url\":\"http://aaa.com/tory/MeetAttents/808491.zip\",\"Time\":\"2017-09-14 15:23\"}]},\"ID\":\"6fa7eef5-735-4f1-a992-8088250be948\",\"Type\":\"专题协调会议\",\"Title\":\"就像在8888\",\"Address\":\"金融港-302会议室\",\"Time\":\"2017-09-14 14:00-15:00\",\"Note\":\"好想到你 \",\"Status\":\"已结束\",\"Author\":\"总经理A\",\"CreatedTime\":\"2017-09-14 14:48\",\"QrCode\":\"eyJ0eXBlIjozLCJpZCI6IjZmYThOTkyLTgwODgCx5YOP5ZyoODg4OCJ9\",\"Action\":\"[VIEW][RECORDER]\"}";

解析代码:(只做了json不同级的数据结构解析,剩下的可以自行在学习过程中去完成)

private void parseJson() {String parseString = "解析:\n"; //GsonBuilder().enableComplexMapKeySerialization().create() 处理Map的key为复杂对象 Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); Type type = new TypeToken() { }.getType(); LinkedTreeMap linkedTreeMap = gson.fromJson(jsonString, type); for (String str : linkedTreeMap.keySet()) {//一级 if (str.equals("Action")) { String actionStr = (String) linkedTreeMap.get(str); parseString += "Action:" + actionStr + "\n"; } else if (str.equals("Address")) { String addressStr = (String) linkedTreeMap.get(str); parseString += "Address:" + addressStr + "\n"; } else if (str.equals("Author")) { String authorStr = (String) linkedTreeMap.get(str); parseString += "Author:" + authorStr + "\n"; } else if (str.equals("CreatedTime")) { String createdTimeStr = (String) linkedTreeMap.get(str); parseString += "CreatedTime:" + createdTimeStr + "\n"; } else if (str.equals("ID")) { String iDStr = (String) linkedTreeMap.get(str); parseString += "ID:" + iDStr + "\n"; } else if (str.equals("Title")) { String titleStr = (String) linkedTreeMap.get(str); parseString += "Title:" + titleStr + "\n"; } else if (str.equals("Type")) { String typeStr = (String) linkedTreeMap.get(str); parseString += "Type:" + typeStr + "\n"; } else if (str.equals("Status")) { String statusStr = (String) linkedTreeMap.get(str); parseString += "Status:" + statusStr + "\n"; } else if (str.equals("QrCode")) { String qrCodeStr = (String) linkedTreeMap.get(str); parseString += "QrCode:" + qrCodeStr + "\n"; } else if (str.equals("Attachments")) {//二级 //1.获到LinkedTreeMap LinkedTreeMap attachmentsStr = (LinkedTreeMap) linkedTreeMap.get(str); //2.将LinkedTreeMap转成String String attachmentsJson = gson.toJson(attachmentsStr); Type type1 = new TypeToken() { }.getType(); //3.再将String转成LinkedTreeMap //为什么不能直接用上面1.LinkedTreeMap,因为它无法直接转化list里面的对象为AttachmentsItemBean com.google.gson.internal.LinkedTreeMap attachments = gson.fromJson(attachmentsJson, type1); for (String attachmentsKey : attachments.keySet()) { List attachmentsItemBeenList = attachments.get(attachmentsKey); for (int i = 0; i < attachmentsItemBeenList.size(); i++) { AttachmentsItemBean attachmentsItemBean = attachmentsItemBeenList.get(i); String actor = attachmentsItemBean.getActor(); String file = attachmentsItemBean.getFile(); String url = attachmentsItemBean.getUrl(); parseString += "\t\t\t--items:" + "actor:" + actor + "\n" + "\t\t\t--file:" + file + "\n" + "\t\t\t--url:" + url + "\n"; } }} else if (str.equals("Members")) {//三级 //1.获到LinkedTreeMap LinkedTreeMap LinkedTreeMapMember_s = (LinkedTreeMap) linkedTreeMap.get(str); //2.将LinkedTreeMap转成String String membersStr = gson.toJson(LinkedTreeMapMember_s); //TypeToken类型转换 Type type2 = new TypeToken() { }.getType(); //3.再将String转成LinkedTreeMap //为什么不能直接用上面1.LinkedTreeMap,因为它无法直接转化list里面的对象为AttachmentsItemBean com.google.gson.internal.LinkedTreeMap LinkedTreeMapMembers = gson.fromJson(membersStr, type2); for (String membersKey : LinkedTreeMapMembers.keySet()) { if (membersKey.equals("Host")) { LinkedTreeMap host = (LinkedTreeMap) LinkedTreeMapMembers.get(membersKey); String hostJson = gson.toJson(host); LinkedTreeMap hostBean = gson.fromJson(hostJson, new TypeToken() { }.getType()); for (String hoststr : hostBean.keySet()) { String hostValue = https://www.it610.com/article/hostBean.get(hoststr); parseString +="\t\t\t----Host----" + hoststr + ":" + hostValue + "\n"; } } } } else if (str.equals("Room")) { LinkedTreeMap roomMap_ = (LinkedTreeMap) linkedTreeMap.get(str); String roomStr = gson.toJson(roomMap_); Type type3 = new TypeToken() { }.getType(); LinkedTreeMap roomMap = gson.fromJson(roomStr, type3); for (String roomKey : roomMap.keySet()) { String roomValue = https://www.it610.com/article/roomMap.get(roomKey); parseString +="\t\t\t--Room:" + roomKey + ":" + roomValue + "\n"; } } } tv.setText(parseString); }


输出的结果:

解析: ----Host----ID:29 ----Host----LoginName:gm3 ----Host----Name:总经理C ----Host----Email: --Room:MeetingID:6fa7eef5-6735-4fb1-a992-8088250be948 --Room:MeetingRoomID:2ebbe73b-bee4-4a9e-81c9-f8ffc6ca1149 --Room:Address:金融港-302会议室 --Room:Date:2017-09-14 --Room:Text:2017-09-14 14:00-15:00 --Room:Value:[14:00][14:30] --items: actor:总经理A --file:1503808491.zip --url:http://aaa.com/tory/MeetAttents/808491.zip ID:6fa7eef5-735-4f1-a992-8088250be948 Type:专题协调会议 Title:就像在8888 Address:金融港-302会议室 Status:已结束 Author:总经理A CreatedTime:2017-09-14 14:48 QrCode:eyJ0eXBlIjozLCJpZCI6IjZmYThOTkyLTgwODgCx5YOP5ZyoODg4OCJ9 Action:[VIEW][RECORDER]


源代码Demo下载

    推荐阅读