Json解析之-実際開発中の特殊需要処理(手動解析-JSONObjectとJSONArrayで処理する)

5519 ワード

どのJsonデータに対してもこの生の方法で解析できます.http://blog.csdn.net/jason_996/article/details/78039153)
使用(org.json.JSONArrayとorg.json.JSONObjectのバッグ):
1、{}に会ったら、JSONObjectで処理します.
2、[]が配列を表し、JSONArrayで処理されます.
以下はJsonデータです.
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]\"}";
    
解析コード:(同じクラスではないものだけを解析して、残りの勉強中に完成します.)
private void parseJsonTwo() {
        String parseStringTwo = "  :
"; JSONObject jsonObject = null; try { jsonObject = new JSONObject(jsonString); // String action = jsonObject.optString("Action"); String id = jsonObject.optString("ID"); String type = jsonObject.optString("Type"); String title = jsonObject.optString("Title"); String address = jsonObject.optString("Address"); parseStringTwo += "Action:" + action + "
ID:" + id + "
Type:" + type + "
Title:" + title + "
Address:" + address + "
"; // , Attachments JSONObject attachments = jsonObject.getJSONObject("Attachments"); JSONArray items = attachments.getJSONArray("Items"); for (int i = 0; i < items.length(); i++) { JSONObject item = items.getJSONObject(i); String actor = item.optString("Actor"); String file = item.optString("File"); parseStringTwo += "Attachments--:
" + "Actor:" + actor + "
File:" + file + "
"; } // , Members--Members JSONObject membersObj = jsonObject.getJSONObject("Members"); JSONArray membersArr = membersObj.getJSONArray("Members"); for (int ii = 0; ii < membersArr.length(); ii++) { JSONObject membersItemObj = membersArr.getJSONObject(ii); String email = membersItemObj.optString("Email"); String loginName = membersItemObj.optString("LoginName"); String name = membersItemObj.optString("Name"); parseStringTwo += "Members--Members:
" + "Email:" + email + "
LoginName:" + loginName + "
Name" + name + "
"; } } catch (JSONException e) { e.printStackTrace(); } tv.setText(parseStringTwo); }
出力結果:
  :
    Action:[VIEW][RECORDER]
    ID:6fa7eef5-735-4f1-a992-8088250be948
    Type:      
    Title:   8888
    Address:   -302   
    Attachments--:
        Actor:   A
        File:1503808491.zip
    Members--Members:
        Email:[email protected]
        LoginName:gm1
        Name   A
    Members--Members:
        Email:[email protected]
        LoginName:gm2
        Name   B
    Members--Members:
        Email:
        LoginName:gm3
        Name   C
ソースコードのダウンロード