0

I am developing a quiz application and my questions are in a Json file

{
"name": "JHS ASANTE TWI",
"course_id": "73",
"courseID": "01JS88",
  "package_code": "JHS",
  "description": "",
  "category": "",
  "author": "content@exams.com",


"questions": {
    "58242": {
      "qid": "58242",
      "topic": "1268",
      "instructions": "Yi nea εne nea yεasensane aseε no asekyerε bɔ abira",
      "text": "<p>Kofi de sika no maa&nbsp;<span style=\"text-decoration: underline;\">aberante<span>&epsilon;</span></span>&nbsp;bi.</p>",
      "resource": "",
      "qtype": "SINGLE",
      "confirmed": "1",
      "public": "1",
      "flagged": "0",
      "updated_at": "2014-07-10 12:29:33",
      "rating": 0,
      "answers": [
        {
          "id": "250310",
          "text": "<p>ababaawa</p>",
          "value": "1",
          "solution": ""
        },
        {
          "id": "250311",
          "text": "<p>ab<span>ɔfra</span><span><br /></span></p>",
          "value": "0",
          "solution": ""
        },
        {
          "id": "250312",
          "text": "<p>aberewa</p>",
          "value": "0",
          "solution": ""
        },
        {
          "id": "250313",
          "text": "<p>abarimaa</p>",
          "value": "0",
          "solution": ""
        }
      ]
    },
    "58245": {
      "qid": "58245",
      "topic": "1268",
      "instructions": "Yi nea εne nea yεasensane aseε no asekyerε bɔ abira",
      "text": "<p>Mehunuu m'adamfo bi nnora&nbsp;<span style=\"text-decoration: underline;\">an</span><span style=\"text-decoration: underline;\">ɔpa</span>.</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>",
      "resource": "",
      "qtype": "SINGLE",
      "confirmed": "1",
      "public": "1",
      "flagged": "0",
      "updated_at": "2014-07-10 12:43:29",
      "rating": 0,
      "answers": [
        {
          "id": "250329",
          "text": "<p>awia</p>",
          "value": "1",
          "solution": ""
        },
        {
          "id": "250328",
          "text": "<p>anwummer<strong></strong>&epsilon;</p>",
          "value": "0",
          "solution": ""
        },
        {
          "id": "250327",
          "text": "<p>ahemadakye</p>",
          "value": "0",
          "solution": ""
        },
        {
          "id": "250326",
          "text": "<p>owigyinae&epsilon;</p>",
          "value": "0",
          "solution": ""
        }
      ]
    },
    "58246": {
      "qid": "58246",
      "topic": "1268",
      "instructions": "Yi nea εne nea yεasensane aseε no asekyerε bɔ abira",
      "text": "<p>Asomaning&nbsp;<span style=\"text-decoration: underline;\">kuro</span> no so.</p>",
      "resource": "",
      "qtype": "SINGLE",
      "confirmed": "1",
      "public": "1",
      "flagged": "0",
      "updated_at": "2014-07-10 12:53:00",
      "rating": 0,
      "answers": [
        {
          "id": "250330",
          "text": "<p><span>ɔmansini</span></p>",
          "value": "0",
          "solution": ""
        },
        {
          "id": "250331",
          "text": "<p><span>ɔman</span></p>",
          "value": "0",
          "solution": ""
        },
        {
          "id": "250332",
          "text": "<p>wiase</p>",
          "value": "0",
          "solution": ""
        },
        {
          "id": "250333",
          "text": "<p>akuraa</p>",
          "value": "1",
          "solution": ""
        }
      ]
    }

  }

}

My question is: how do I access the next question of "questions" when i click next for the next question since "58242", "58245", etc are all dynamic values?

the answer on this post was very helpfull but now i want to now how loop through the questions How to parse a dynamic JSON key in a Nested JSON result

it helped me get the currentdynamic value but does not give me the next question when i click on next

private View.OnClickListener nextListener = new View.OnClickListener() {
        public void onClick(View v) {
            setAnswer();
            try {
                if (quesIndex != searchResult.getJSONObject("questions").length() - 1) {
                    quesIndex++;
                    progress.setProgress(0);
                    progress.setMax(100);
                } else {
                    Intent myIntent = new Intent(TestActivity.this, Assesment.class);
                    myIntent.putExtra("intVariableName", score);
                    startActivity(myIntent);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                if (quesIndex == searchResult.getJSONObject("questions").length() - 1) {
                    next.setText("Finish");

                    Intent myIntent = new Intent(TestActivity.this, Assesment.class);
                    myIntent.putExtra("intVariableName", score);
                    startActivity(myIntent);
                    // close this activity
                    finish();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            showQuestion(quesIndex, review);
        }
    };

will appreciate the help thanks :)

Community
  • 1
  • 1
SoftServe
  • 67
  • 13
  • as a side note: json objects are not ordered. – njzk2 Dec 17 '14 at 17:03
  • Can you modify your json? If questions was an array, it would be simple to parse as an ArrayList and iterate through – Biniou Dec 17 '14 at 17:12
  • @Biniou Yeah taught of that but am using the same json file for the web version so changing it might will affect the web version plus its a lot to change :) – SoftServe Dec 17 '14 at 17:37

1 Answers1

0

Not sure what you problem is. Seems like the solution is in the link you gave above.

JSONObject questions = result.getJSONObject("questions");
Iterator keys = questions.keys();

//go through every question
while (keys.hasNext()) {
    String currentDynamicKey = (String)keys.next();
    JSONObject currentQuestion = questions.getJSONObject(currentDynamicKey);
}
Biniou
  • 133
  • 1
  • 9