如何在android中的Json文件中获取问题

时间:2014-12-09 13:21:03

标签: android json

我正在开发一个测验应用程序,从Json文件(复杂)

中获取问题和主题
  { 
  "name": "JHS ASANTE TWI",
  "course_id": "73",
  "courseID": "01JS88",
  "package_code": "JHS",
  "description": "",
  "category": "",

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


  "updated_at": "2014-10-28 16:07:54",
  "nquestions": 788
}

如何在我的应用中获得所有问题?

这是我写的代码,但它只在&#34;问题&#34;

下调用了一个问题
//Method that will parse the JSON file and will return a JSONObject
     public String loadJSONFromRaw() {
            String json = null;
            try {

                InputStream questions = this.getBaseContext().getResources()
                        .openRawResource(R.raw.document);

                int size = questions.available();

                byte[] buffer = new byte[size];

                questions.read(buffer);

                questions.close();

                json = new String(buffer, "UTF-8");


            } catch (IOException ex) {
                ex.printStackTrace();
                return null;
            }
            return Html.fromHtml(json).toString();

        }


    //this calls and executes the code above

private void showQuestion(int qIndex, boolean review) {
        try {
            obj = new JSONObject(loadJSONFromAsset());
            String header =  obj.getString("name");
            Log.v("name--", obj.getString("name"));
            System.out.println(header);
            subject.setText(header);
            //JSONObject aQues = QuestionPackage.getQuesList().getJSONObject(qIndex);
            String instructions = obj.getJSONObject("questions").getJSONObject("58242").getString("instructions");
            intruction.setText(instructions);
            String quesValue = obj.getJSONObject("questions").getJSONObject("58242").getString("text");
           // String quesValue = myString.getString("name");
            questionView.setText(quesValue);
            System.out.println(quesValue);
            if (correctAns[qIndex] == -1) {
                String correctAnsStr =  obj.getJSONObject("questions").getJSONObject("58242").getString("confirmed");
                correctAns[qIndex] = Integer.parseInt(correctAnsStr);
            }
            //subject.setText(header);

            answers.check(-1);
            optionone.setTextColor(Color.BLACK);
            optiontwo.setTextColor(Color.BLACK);
            optionthree.setTextColor(Color.BLACK);
            optionfour.setTextColor(Color.BLACK);
            JSONArray ansList = obj.getJSONObject("questions").getJSONObject("58242").getJSONArray("answers");
            String aAns = ansList.getJSONObject(0).getString("text");
            optionone.setText(aAns.toCharArray(), 0, aAns.length());
            aAns = ansList.getJSONObject(1).getString("text");
            optiontwo.setText(aAns.toCharArray(), 0, aAns.length());
            aAns = ansList.getJSONObject(2).getString("text");
            optionthree.setText(aAns.toCharArray(), 0, aAns.length());
            aAns = ansList.getJSONObject(3).getString("text");
           optionfour.setText(aAns.toCharArray(), 0, aAns.length());
            Log.d("", selected[qIndex] + "");
            if (selected[qIndex] == 0)
                answers.check(R.id.optionone);
            if (selected[qIndex] == 1)
                answers.check(R.id.optiontwo);
            if (selected[qIndex] == 2)
                answers.check(R.id.optionthree);
            if (selected[qIndex] == 3)
                answers.check(R.id.optionfour);

            setScoreTitle();
            if (quesIndex == (QuestionPackage.getQuesList().length() - 1))
                next.setEnabled(false);

            if (quesIndex == 0)
                back.setEnabled(false);

            if (quesIndex > 0)
                back.setEnabled(true);

            if (quesIndex < (QuestionPackage.getQuesList().length() - 1))
                next.setEnabled(true);


            if (review) {
                Log.d("review", selected[qIndex] + "" + correctAns[qIndex]);
                if (selected[qIndex] != correctAns[qIndex]) {
                    if (selected[qIndex] == 0)
                        optionone.setTextColor(Color.RED);
                    if (selected[qIndex] == 1)
                        optiontwo.setTextColor(Color.RED);
                    if (selected[qIndex] == 2)
                        optionthree.setTextColor(Color.RED);
                    if (selected[qIndex] == 3)
                        optionfour.setTextColor(Color.RED);
                }
                if (correctAns[qIndex] == 0)
                    optionone.setTextColor(Color.GREEN);
                if (correctAns[qIndex] == 1)
                    optiontwo.setTextColor(Color.GREEN);
                if (correctAns[qIndex] == 2)
                    optionthree.setTextColor(Color.GREEN);
                if (correctAns[qIndex] == 3)
                    optionfour.setTextColor(Color.GREEN);
            }
        } catch (Exception e) {
            Log.e(this.getClass().toString(), e.getMessage(), e.getCause());
        }
    }

1 个答案:

答案 0 :(得分:0)

如何在我的应用中获得所有问题?

更简单的方法是使用gson库[1]:https://sites.google.com/site/gson/gson-user-guide

您可以将JSON字符串转换为等效的Java对象并开始阅读该对象。