如何在android中的嵌套json对象中循环动态json键

时间:2014-12-17 17:00:13

标签: android json

我正在开发一个测验应用程序,我的问题在Json文件中

{
"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": ""
        }
      ]
    }

  }

}

我的问题是:我如何访问下一个问题&#34;问题&#34;当我点击下一个问题时,因为&#34; 58242&#34;,&#34; 58245&#34;等都是动态值?

这篇文章的答案非常有用,但现在我想现在如何循环问题How to parse a dynamic JSON key in a Nested JSON result

它帮助我获得了当前的动态值,但是当我点击下一个

时,它没有给我下一个问题
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);
        }
    };

将非常感谢帮助谢谢:)

1 个答案:

答案 0 :(得分:0)

不确定您的问题是什么。似乎解决方案在您上面给出的链接中。

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);
}