0

我得到这样的 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

4

2 回答 2

1

只需像这样使用两个豆子:问题:

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

然后你就拥有了所有QuestionslistQuestions; 你所要做的就是添加一个循环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());
   }
}
于 2012-11-09T17:47:22.490 回答
1

您可以做几件事来实现这一目标。

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

    类条目{
        字符串问题;
        字符串 [] 选项;
    }
    //将所有已解析的条目保存到数组列表中
    ArrayList 条目;
    
  2. 为了制作 UI,现在您可以迭代每个“条目”并使用文本视图和单选按钮显示选项。- 所以你可以管理整个问题'要么在一个视图组中,要么有单独的导航
于 2012-11-09T16:03:05.760 回答