如何从android中获取以下Json响应的值

时间:2012-11-09 15:28:12

标签: android json

我得到了像这样的Json回复,

{
    "data": {
        "id": "1",
        "name": "General Knowlege Questions",
        "description": "This questions will test your General ",
        "questions": [
            {
                "id": "1",
                "text": "Who invented the BALLPOINT PEN?",
                "type": "2",
                "answers": [
                    {
                        "id": "1",
                        "text": "Biro Brothers"
                    },
                    {
                        "id": "2",
                        "text": "Waterman Brothers"
                    },
                    {
                        "id": "3",
                        "text": "Bicc Brothers"
                    },
                    {
                        "id": "4",
                        "text": "Write Brothers "
                    }
                ]
            },
            {
                "id": "2",
                "text": "What J. B. Dunlop invented?",
                "type": "2",
                "answers": [
                    {
                        "id": "5",
                        "text": "Pneumatic rubber tire"
                    },
                    {
                        "id": "6",
                        "text": "Automobile wheel rim"
                    },
                    {
                        "id": "7",
                        "text": "Rubber boot"
                    },
                    {
                        "id": "8",
                        "text": "Model airplanes"
                    }
                ]
            },
            {
                "id": "3",
                "text": "Which scientist discovered the radioactive element radium?",
                "type": "2",
                "answers": [
                    {
                        "id": "9",
                        "text": "Isaac Newton"
                    },
                    {
                        "id": "10",
                        "text": "Albert Einstein"
                    },
                    {
                        "id": "11",
                        "text": "Benjamin Franklin"
                    },
                    {
                        "id": "12",
                        "text": "Marie Curie"
                    }
                ]
            },
            {
                "id": "4",
                "text": "What now-ubiquitous device was invented by Zenith engineer Eugene Polley in 1955?",
                "type": "1",
                "answers": [
                    {
                        "id": "13",
                        "text": "Microwave oven"
                    },
                    {
                        "id": "14",
                        "text": "Remote control"
                    }
                ]
            },
            {
                "id": "5",
                "text": "What Benjamin Franklin invented?",
                "type": "1",
                "answers": [
                    {
                        "id": "15",
                        "text": "Bifocal spectacles"
                    },
                    {
                        "id": "16",
                        "text": "Radio"
                    }
                ]
            }
        ]
    }
}

我可以使用以下代码从此响应中获取所有数据,遗憾的是我无法显示适合问题的答案集。

我的代码段是,

public void getQuestions(String survey_response) {
    try {
        JSONObject first_obj = new JSONObject(survey_response);
        String data_stirng = first_obj.getString("data");
        JSONObject sub_obj = new JSONObject(data_stirng);
        String name_val = sub_obj.getString("questions");
        JSONArray questions_array = new JSONArray(name_val);
        for (int i = 0; i < questions_array.length(); i++) {
            JSONObject qus_elements = questions_array.getJSONObject(i);
            QUESTION_ID = qus_elements.getString("id");
            QUESTION_TEXT = qus_elements.getString("text");
            QUESTION_TYPE = qus_elements.getString("type");
            String answers_val = qus_elements.getString("answers");
            JSONArray ans_array = new JSONArray(answers_val);
            Log.v("Answers Array Values", ans_array + "");
            for (int j = 0; j < ans_array.length(); j++) {

                JSONObject ans_elements = ans_array.getJSONObject(j);
                ANSWERS_ID = ans_elements.getString("id");
                ANSWERS_TEXT = ans_elements.getString("text");
                answers_id.add(ANSWERS_ID);
                answers_text.add(ANSWERS_TEXT);
            }

            ques_id.add(QUESTION_ID);
            ques_text.add(QUESTION_TEXT);
            ques_type.add(QUESTION_TYPE);
        }
        // Log.v("QUESTION ID ARRAY", ques_id + "");
        // Log.v("QUESTION Text ARRAY", ques_text + "");
        // Log.v("QUESTION type ARRAY", ques_type + "");
        Log.v("ANSWERS ID ARRAY", answers_id + "");
        Log.v("ANSWERS TEXT ARRAY", answers_text + "");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我需要像这样展示这个回复,我不知道如何用这种格式显示问题和答案。

sktech

2 个答案:

答案 0 :(得分:1)

你可以做很多事情来实现这个目标。

  1. 有一个简单的模型类,如

    class Entry{
        String question;
        String[] options;
    }
    //Hold all your parsed entries to the array list
    ArrayList entries;
    
  2. 为了制作用户界面,现在您可以使用文本视图和单选按钮迭代每个“条目”并显示选项。 - 所以你可以在一个ViewGroup中管理整个问题,或者有单独的导航

答案 1 :(得分:1)

只需使用两个这样的bean: 问题:

public class Question {
  private int id;
  private String text;
  private int type;
  private ArrayList<Answer> answers = new ArrayList<Answer>();

  public Question( int id, String text, int type, ArrayList<Answer> answers) {
     this.id = id;
     this.text = text;
     this.type = type;
     this.answers = answers;
  }
  //TODO Getters and Setters
}

答案:

public class Answer {
  private int id;
  private String text;

  public Answer(int id, String text) {
     this.id = id;
     this.text = text;
  }
  //TODO Getters and Setters
}

并在解析问题和解答时执行此操作:

ArrayList<Question> listQuestions = new ArrayList<Question>();

    JSONArray questions_array = sub_obj.getJsonArray("questions");
            for (int i = 0; i < questions_array.length(); i++) {
                JSONObject jsonQuestion = questions_array.getJSONObject(i);
                Question q = new Question();
                q.setId(jsonQuestion.optInt("id",-1));
                q.setText(jsonQuestion.optString("text",null));
                q.setType(jsonQuestion.optInt("type",-1));
                JSONArray ans_array = jsonQuestion.getJsonArray("answers");
                Log.v("Answers Array Values", ans_array + "");
                for (int j = 0; j < ans_array.length(); j++) {

                    JSONObject jsonAnswer = ans_array.getJSONObject(j);
                    Answer a = new Answer();
                    a.setId(jsonAnswer.optInt("id",-1);
                    a.setText(jsonAnswer.optString("text"),null);
                    q.getAnswers().add(a);
                }

                listQuestions.add(q);
            }

然后你在Questions中拥有了所有listQuestions;所有你要做的就是添加一个循环For,你会得到你的问题,每个问题都有q.getAnswers()这样的答案:

for(int l = 0; l< listQuestions.size(); l++ ){
   Question currentQuestion = listQuestions.get(l);
   Log.i("QCMActivity", "the Question : "+currentQuestion.getText());
   ArrayList<Answer> answersOfCurrentQuestion = currentQuestion.getAnswers();
   Log.i("QCMActivity", "Answers : ");
   for( int k = 0; k< answersOfCurrentQuestion.size(); k++) {
       Answer currentAnswer = answersOfCurrentQuestion.get(k);
       Log.i("QCMActivity", "Option "+(k+1)+" : "+currentAnswer.getText());
   }
}