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

如何在android中获取Json文件中的问题,android,json,Android,Json,我正在开发一个测验应用程序,它从Json文件(复杂)中获取问题和主题 { “名称”:“JHS ASANTE TWI”, “课程id”:“73”, “courseID”:“01JS88”, “包装代码”:“JHS”, “说明”:“, “类别”:“, “问题”:{ "58242": { “qid”:“58242”, “主题”:“1268”, “说明”:“Yi neaεne nea yεasensane aseεno asekyerεbɔabira”, “文本”:“Kofi de sika no ma

我正在开发一个测验应用程序,它从Json文件(复杂)中获取问题和主题

{
“名称”:“JHS ASANTE TWI”,
“课程id”:“73”,
“courseID”:“01JS88”,
“包装代码”:“JHS”,
“说明”:“,
“类别”:“,
“问题”:{
"58242": {
“qid”:“58242”,
“主题”:“1268”,
“说明”:“Yi neaεne nea yεasensane aseεno asekyerεbɔabira”,
“文本”:“Kofi de sika no maa aberanteεbi.

”, “资源”:“, “qtype”:“SINGLE”, “已确认”:“1”, “公众”:“1”, “已标记”:“0”, 更新日期:“2014-07-10 12:29:33”, “评级”:0, “答案”:[ { “id”:“250310”, “正文”:“ababawa

”, “值”:“1”, “解决方案”:” }, { “id”:“250311”, “文本”:“abɔfra

”, “值”:“0”, “解决方案”:” }, { “id”:“250312”, “文本”:“aberewa

”, “值”:“0”, “解决方案”:” }, { “id”:“250313”, “文本”:“abarimaa

”, “值”:“0”, “解决方案”:” } ] }, "58245": { “qid”:“58245”, “主题”:“1268”, “说明”:“Yi neaεne nea yεasensane aseεno asekyerεbɔabira”, “正文”:“Mehunuu m'adamfo bi nnora anɔpa.

\n\n”, “资源”:“, “qtype”:“SINGLE”, “已确认”:“1”, “公众”:“1”, “已标记”:“0”, 更新日期:“2014-07-10 12:43:29”, “评级”:0, “答案”:[ { “id”:“250329”, “文本”:“awia

”, “值”:“1”, “解决方案”:” }, { “id”:“250328”, “文本”:“anwummerε

”, “值”:“0”, “解决方案”:” }, { “id”:“250327”, “文本”:“ahemadakye

”, “值”:“0”, “解决方案”:” }, { “id”:“250326”, “文本”:“owigyinaeε

”, “值”:“0”, “解决方案”:” } ] } }, 更新日期:“2014-10-28 16:07:54”, “质询”:788 }

如何获取应用程序中的所有问题

这是我写的代码,但它只在“问题”下调用一个问题

//将解析JSON文件并返回JSONObject的方法
公共字符串loadJSONFromRaw(){
字符串json=null;
试一试{
InputStream questions=this.getBaseContext().getResources()
.openrawsource(R.raw.document);
int size=questions.available();
字节[]缓冲区=新字节[大小];
问题。阅读(缓冲区);
问题。结束();
json=新字符串(缓冲区,“UTF-8”);
}捕获(IOEX异常){
例如printStackTrace();
返回null;
}
返回Html.fromHtml(json).toString();
}
//这将调用并执行上面的代码
私有void showQuestion(int-qIndex,boolean review){
试一试{
obj=新的JSONObject(loadJSONFromAsset());
字符串头=obj.getString(“名称”);
Log.v(“name--”,obj.getString(“name”);
系统输出打印项次(表头);
subject.setText(标题);
//JSONObject aQues=QuestionPackage.getQuesList().getJSONObject(qIndex);
字符串指令=obj.getJSONObject(“问题”).getJSONObject(“58242”).getString(“指令”);
intuition.setText(说明);
String quesValue=obj.getJSONObject(“问题”).getJSONObject(“58242”).getString(“文本”);
//字符串quesValue=myString.getString(“名称”);
questionView.setText(quesValue);
System.out.println(quesValue);
如果(更正[qIndex]=-1){
String correctAnsStr=obj.getJSONObject(“问题”).getJSONObject(“58242”).getString(“确认”);
correctAns[qIndex]=Integer.parseInt(correctAnsStr);
}
//subject.setText(标题);
答案。勾选(-1);
optionone.setTextColor(颜色:黑色);
选项2.setTextColor(颜色为黑色);
选项tree.setTextColor(Color.BLACK);
选项4.setTextColor(颜色为黑色);
JSONArray ansList=obj.getJSONObject(“问题”).getJSONObject(“58242”).getJSONArray(“答案”);
字符串aAns=ansList.getJSONObject(0.getString(“文本”);
optionone.setText(aAns.tocharray(),0,aAns.length());
aAns=ansList.getJSONObject(1.getString(“文本”);
optiontwo.setText(aAns.tocharray(),0,aAns.length());
aAns=ansList.getJSONObject(2.getString(“文本”);
optionTree.setText(aAns.tocharray(),0,aAns.length());
aAns=ansList.getJSONObject(3.getString(“文本”);
optionfour.setText(aAns.tocharray(),0,aAns.length());
Log.d(“”,选择[qIndex]+”);
如果(选择[qIndex]==0)
答案。检查(R.id.optionone);
如果(选择[qIndex]==1)
答案。检查(R.id.optiontwo);
如果(所选[qIndex]==2)
答案。勾选(R.id.选项树);
如果(选择[qIndex]==3)
答案。勾选(R.id.选项四);
setScoreTitle();
if(quessindex==(QuestionPackage.getQuesList().length()-1))
next.setEnabled(false);
如果(quesIndex==0)
back.setEnabled(false);
如果(quesIndex>0)
back.setEnabled(true);
if(quesIndex<(QuestionPackage.getQuesList().length()-1))
next.setEnabled(true);
如果(审查)
  { 
  "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
}
//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());
        }
    }