我也是JSON和android的新手,我没有得到如何解析内部“选项”JSONArray

时间:2013-08-23 10:56:36

标签: android json

{
"question": [
    {
        "no": 1,
        "question_text": "how many keys are on a standard computer keyboard?",
        "answer": "104",
        "options": [
            {
                "option": "106"
            },
            {
                "option": "104"
            },
            {
                "option": "105"
            },
            {
                "option": "108"
            }
        ]
    },
    {
        "no": 2,
        "question_text": "In which planet we live?",
        "answer": "Earth",
        "options": [
            {
                "option": "Mars"
            },
            {
                "option": "Jupiter"
            },
            {
                "option": "Earth"
            },
            {
                "option": "Saturn"
            }
        ]
    },
    {
        "no": 3,
        "question_text": "Which is the recent novel released by Chethan Bhagat? ",
        "answer": "What young India wants",
        "options": [
            {
                "option": "Revolution 2020"
            },
            {
                "option": "What young India wants"
            },
            {
                "option": "Three mistakes of my Life"
            },
            {
                "option": "One night at call center"
            }
        ]
    },
    {
        "no": 4,
        "question_text": "Which among these is the smallest state in India?",
        "answer": "Goa",
        "options": [
            {
                "option": "Nagaland"
            },
            {
                "option": "Goa"
            },
            {
                "option": "Orisaa"
            },
            {
                "option": "Kerala"
            }
        ]
    },
    {
        "no": 5,
        "question_text": "Lunar Eclipse occurs on?",
        "answer": "A full moon Day",
        "options": [
            {
                "option": "A new moon Day"
            },
            {
                "option": "A half moon Day"
            },
            {
                "option": "A full moon Day"
            },
            {
                "option": "Both a and b"
            }
        ]
    }
 ]
}

这是我的代码,首先我想得到第一个问题和相关的值,如选项,答案。我搜索了它,但我没有得到任何与我的问题相关的解决方案。请帮帮我

private class DetailsTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        Log.i(TAG, "Inside onPreExecute");
        if (!progress_dialog.isShowing())
            progress_dialog.show();
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (progress_dialog.isShowing())
            progress_dialog.dismiss();
        Log.i(TAG, "Inside onPostExecute");

        JSONObject json, data, value = null;
        JSONArray questions_array;

        // getting JSON string from context
        json = parser.getJSONFromUrl(context);
        Log.i(TAG, "" + json.toString());

        try {

            questions_array = json.getJSONArray(TAG_QUESTION_ARRAY);

            for (int i = 0; i < questions_array.length(); i++) {
                data = questions_array.getJSONObject(i);


                String question_text = data.getString(TAG_QUESTION);
                model.setQuestion(question_text);

                int question_num = data.getInt(TAG_QUESTION_NO);
                model.setQuestion_number(question_num);


                String answer = data.getString(TAG_ANSWER);
                model.setRight_answer(answer);

                // options is again an array
                JSONArray q_options = data.getJSONArray(TAG_OPTIONS_ARRAY);
                for (int j = 0; j < q_options.length(); j++) {
                    value = q_options.getJSONObject(j);

                }

                String option = value.getString(TAG_OPTION);
                question.setText(model.getOption());
                question_number.setText(model.getQuestion_number());
                //Here i want to set 4 options to 4 different textviews

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

    }

}

private class ClickListener implements View.OnClickListener {

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        if (view instanceof Button) {
            Button temp = (Button) view;
            id = temp.getId();
        } else {
            TextView temp = (TextView) view;
            id = temp.getId();
            Log.i(TAG, "value of id:" + id);

            currenttext = ((TextView) view).getText().toString();
            Log.i(TAG, "value of string:" + currenttext);

        }

        switch (id) {
        case R.id.button_next:
            Toast.makeText(context, "Next pressed", Toast.LENGTH_SHORT)
                    .show();
            int qu = model.getQuestion_number();
            int new_qn = qu + 1;
            Log.i(TAG, "qqqqqqqqnnnn:::----" + new_qn);
            // set values for question number
            break;

        case R.id.button_previous:
            Toast.makeText(context, "Previous pressed", Toast.LENGTH_SHORT)
                    .show();
            int qu1 = model.getQuestion_number();
            int new_qn1 = qu1 - 1;
            Log.i(TAG, "qqqqqqqqnnnn:::----" + new_qn1);
            break;

        case R.id.first_option:
            getScore();
            break;

        case R.id.second_option:
            getScore();
            break;

        case R.id.third_option:
            getScore();
            break;

        case R.id.fourth_option:
            getScore();
            break;

        }
    }
}
}

我想将这些选项值设置为4个不同的文本视图,当用户点击任何文本视图(可点击)时,我想显示下一个问题及其相关选项,但我没有得到如何解析这个“选项” JSONArray

1 个答案:

答案 0 :(得分:0)

ArrayList<String> ls_options = new Arr...


for (int j = 0; j < q_options.length(); j++) {
      //  value = q_options.getJSONObject(j);
          ls_options.add(q_options.getJSONObject(j));

}

textview0.setText(ls_options.get(0));
textview1.setText(ls_options.get(1));
textview2.setText(ls_options.get(2));

你创建了value类型JSONObject,你在for-loop中分配它,所以它被覆盖