Json変換利器Gsonの-実際開発中の特殊需要処理(手動解析-Type Token処理)

10409 ワード

開発中に遭遇した複雑なJSONはType Tokenで処理します.http://blog.csdn.net/jason_996/article/details/78039799)
処理方法:
1,まずインターフェースを返すJSONは、gson.tojson(map)方法で、Linked TreeMapオブジェクトをStringに変換します.
2,gson.from json(string,type)方法でSteringをLinked TreeMapに変えます.Type=new Type Token(){}.getType;
3,Linked TreeMapfを巡回します.
下記に直接コードを書きます.(例ではデータの内容はデモと違っていますが、データ構造は同じです.会社の情報を知らせないために)
 private String jsonString = "{\"Members\":{\"Owner\":{\"ID\":\"26\",\"LoginName\":\"gm1\",\"Name\":\"   A\",\"Email\":\"[email protected]\"},\"Host\":{\"ID\":\"29\",\"LoginName\":\"gm3\",\"Name\":\"   C\",\"Email\":\"\"},\"Recorder\":{\"ID\":\"26\",\"LoginName\":\"gm1\",\"Name\":\"   A\",\"Email\":\"[email protected]\"},\"Members\":[{\"ID\":\"26\",\"LoginName\":\"gm1\",\"Name\":\"   A\",\"Email\":\"[email protected]\"},{\"ID\":\"28\",\"LoginName\":\"gm2\",\"Name\":\"   B\",\"Email\":\"[email protected]\"},{\"ID\":\"29\",\"LoginName\":\"gm3\",\"Name\":\"   C\",\"Email\":\"\"}],\"Attendance\":[{\"ID\":\"29\",\"LoginName\":\"gm3\",\"Name\":\"   C\",\"Email\":\"\"}],\"Absence\":[{\"LoginName\":\"gm1\",\"Name\":\"   A\",\"Email\":\"[email protected]\"},{\"LoginName\":\"gm2\",\"Name\":\"   B\",\"Email\":\"[email protected]\"}]},\"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 = "  :
"; //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 + "
"; } else if (str.equals("Address")) { String addressStr = (String) linkedTreeMap.get(str); parseString += "Address:" + addressStr + "
"; } else if (str.equals("Author")) { String authorStr = (String) linkedTreeMap.get(str); parseString += "Author:" + authorStr + "
"; } else if (str.equals("CreatedTime")) { String createdTimeStr = (String) linkedTreeMap.get(str); parseString += "CreatedTime:" + createdTimeStr + "
"; } else if (str.equals("ID")) { String iDStr = (String) linkedTreeMap.get(str); parseString += "ID:" + iDStr + "
"; } else if (str.equals("Title")) { String titleStr = (String) linkedTreeMap.get(str); parseString += "Title:" + titleStr + "
"; } else if (str.equals("Type")) { String typeStr = (String) linkedTreeMap.get(str); parseString += "Type:" + typeStr + "
"; } else if (str.equals("Status")) { String statusStr = (String) linkedTreeMap.get(str); parseString += "Status:" + statusStr + "
"; } else if (str.equals("QrCode")) { String qrCodeStr = (String) linkedTreeMap.get(str); parseString += "QrCode:" + qrCodeStr + "
"; } 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 + "
" + "\t\t\t--file:" + file + "
" + "\t\t\t--url:" + url + "
"; } } } 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 = hostBean.get(hoststr); parseString += "\t\t\t----Host----" + hoststr + ":" + hostValue + "
"; } } } } 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 = roomMap.get(roomKey); parseString += "\t\t\t--Room:" + roomKey + ":" + roomValue + "
"; } } } 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]
ソースコードのダウンロード