无法检索具有子数组多个键值的JSON对象

时间:2018-03-21 05:18:36

标签: android arrays json

我对Android非常陌生。请帮助我将所有数据都放在下面引用的JSON文件中。我能够获得每个JSON对象的JSON,但不能获得标记对象及其内部数组及其键值。
谢谢。

{
    "error": false,
    "terms": [{
        "TermId": "1",
        "TermTitle": "Term1"
    }, {
        "TermId": "2",
        "TermTitle": "Term2"
    }, {
        "TermId": "3",
        "TermTitle": "Term3"
    }],
    "exams": [{
        "TypeId": "1",
        "TypeTitle": "Unit Test"
    }, {
        "TypeId": "2",
        "TypeTitle": "PERIODIC TEST 1"
    }, {
        "TypeId": "3",
        "TypeTitle": "PERIODIC TEST 2"
    }, {
        "TypeId": "4",
        "TypeTitle": "HALF YEARLY EXAM"
    }, {
        "TypeId": "5",
        "TypeTitle": "ANNUAL EXAM"
    }, {
        "TypeId": "6",
        "TypeTitle": "PERIODIC TEST 3"
    }, {
        "TypeId": "7",
        "TypeTitle": "QUARTERLY"
    }],
    "status": 0,
    "exam": {
        "examname": "PT3"
    },
    "marks": {
        "Language 1 English": {
            "Theory": ["", "25", "8", "18", "Passed"]
        },
        "Language 2 Malayalam": {
            "Theory": ["", "25", "8", "12", "Passed"]
        },
        "Language 3 Hindi": {
            "Theory": ["", "25", "8", "6", "Failed"]
        },
        "Mathematics": {
            "Theory": ["", "25", "8", "16", "Passed"]
        },
        "EVS": {
            "Theory": ["", "25", "8", "17", "Passed"]
        },
        "Computer": {
            "Theory": ["", "25", "8", "23", "Passed"]
        },
        "Arabic": {
            "Theory": ["", "25", "8", "18", "Passed"]
        }
    }
}

1 个答案:

答案 0 :(得分:1)

请用它来获取所有内部JsonArray

JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(response);  //Response is your Json Response
        //Getting "error" tag
        Boolean error = jsonObject.getBoolean("error");
        //Getting "terms" JsonArray
        for (int i = 0; i < jsonObject.getJSONArray("terms").length(); i++) {
            JSONObject jsonObject1 = jsonObject.getJSONArray("terms").getJSONObject(i);
            String termId = jsonObject1.getString("TermId");  //Get Term id According to index
            String TermTitle = jsonObject1.getString("TermTitle");  //Get Term Title According to index

        }

        //Getting "exams" JsonArray
        for (int i = 0; i < jsonObject.getJSONArray("exams").length(); i++) {
            JSONObject jsonObject1 = jsonObject.getJSONArray("terms").getJSONObject(i);
            String termId = jsonObject1.getString("TermId");  //Get Term id According to index
            String TermTitle = jsonObject1.getString("TermTitle");  //Get Term Title According to index

        }
        //Getting "status" tag
        int status = jsonObject.getInt("status");

        //Getting "examname" tag
        String  examname = jsonObject.getJSONObject("exam").getString("examname");

        //Getting "marks" Json Object
        JSONObject marksJsonObject = jsonObject.getJSONObject("marks");
        //Getting "exams" JsonArray
        for (int i = 0; i < marksJsonObject.getJSONObject("Language 1 English").getJSONArray("Theory").length(); i++) {
            //JSONObject jsonObject1 = marksJsonObject.getJSONObject("Language 1 English").getJSONArray("Theory").get(i);
            String values = (String) marksJsonObject.getJSONObject("Language 1 English").getJSONArray("Theory").get(i);  //Get each Value According to index

        }

        for (int i = 0; i < marksJsonObject.getJSONObject("Language 2 Malayalam").getJSONArray("Theory").length(); i++) {
            //JSONObject jsonObject1 = marksJsonObject.getJSONObject("Language 1 English").getJSONArray("Theory").get(i);
            String values = (String) marksJsonObject.getJSONObject("Language 2 Malayalam").getJSONArray("Theory").get(i);  //Get each Value According to index

        }

        for (int i = 0; i < marksJsonObject.getJSONObject("Language 3 Hindi").getJSONArray("Theory").length(); i++) {
            //JSONObject jsonObject1 = marksJsonObject.getJSONObject("Language 1 English").getJSONArray("Theory").get(i);
            String values = (String) marksJsonObject.getJSONObject("Language 3 Hindi").getJSONArray("Theory").get(i);  //Get each Value According to index

        }

        for (int i = 0; i < marksJsonObject.getJSONObject("Mathematics").getJSONArray("Theory").length(); i++) {
            //JSONObject jsonObject1 = marksJsonObject.getJSONObject("Language 1 English").getJSONArray("Theory").get(i);
            String values = (String) marksJsonObject.getJSONObject("Mathematics").getJSONArray("Theory").get(i);  //Get each Value According to index

        }

        for (int i = 0; i < marksJsonObject.getJSONObject("EVS").getJSONArray("Theory").length(); i++) {
            //JSONObject jsonObject1 = marksJsonObject.getJSONObject("Language 1 English").getJSONArray("Theory").get(i);
            String values = (String) marksJsonObject.getJSONObject("EVS").getJSONArray("Theory").get(i);  //Get each Value According to index

        }

        for (int i = 0; i < marksJsonObject.getJSONObject("Computer").getJSONArray("Theory").length(); i++) {
            //JSONObject jsonObject1 = marksJsonObject.getJSONObject("Language 1 English").getJSONArray("Theory").get(i);
            String values = (String) marksJsonObject.getJSONObject("Computer").getJSONArray("Theory").get(i);  //Get each Value According to index

        }

        for (int i = 0; i < marksJsonObject.getJSONObject("Arabic").getJSONArray("Theory").length(); i++) {
            //JSONObject jsonObject1 = marksJsonObject.getJSONObject("Language 1 English").getJSONArray("Theory").get(i);
            String values = (String) marksJsonObject.getJSONObject("Arabic").getJSONArray("Theory").get(i);  //Get each Value According to index

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }