2012-11-09 85 views
0

以下JSON響應我得到這樣的JSON響應,如何獲得價值形成的Android

{ 
    "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" 
        } 
       ] 
      } 
     ] 
    } 
} 

我可以能夠從中得到響應的所有數據的使用下面的代碼,遺憾的是我不能t顯示適合問題的答案。

我的代碼片段,

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

+0

您可以添加您logcat的? – Houcine

+0

@Houcine:你需要數組列表? – Aerrow

+0

我只需要看看你是否正確地檢索jsonArrays :)。 注意:你可以直接使用json.getJsonObject(String key);和json.getJsonArray(String key);而不是獲取字符串和instanciate與該字符串 – Houcine

回答

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

然後你有你的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()); 
    } 
} 
+0

@Hocine:它工作正常,當我打印答案我得到這樣的,11-10 08:46:49.790:V/Answers數組值(9709):[com.fsp.acacia.health.Week_Assesment_Activity $ Answer @ 41474e60,[email protected],[email protected],[email protected]] How can我得到了實際的字符串。 – Aerrow

+0

我已經編輯了我的答案,添加了顯示ArrayList數據的方式 – Houcine

+0

非常感謝...... :) – Aerrow

1

有兩件事是可以做,以實現這一目標。

  1. 有像

    class Entry{ 
        String question; 
        String[] options; 
    } 
    //Hold all your parsed entries to the array list 
    ArrayList entries; 
    
  2. 一個簡單的模型類爲了使UI,現在你可以用每一個「入口」和循環利用顯示文本視圖和單選按鈕的選項。 - 所以你可以在任何一個單一的ViewGroup管理整個question'are或有單獨的導航
+0

感謝您的回答,請你多解釋一下, – Aerrow

+0

假設你已經通過step1(讓我知道你是否被困在它本身),現在你有整個JSON解析並添加到ArrayList。有...對於UI你可以做的是...如果你有每個問題的問題和解答1頁面 - 通過傳遞帶有Integer額外的意圖來啓動QuestionActivity。檢查值的存在 - 它會告訴你你想顯示的問題的'索引'。所以,從你的靜態ArrayList - 檢索那個'Entry'。在用戶界面上 - 使用TextView編寫一個RelativeLayout來獲取問題,使用RadioGroup獲得答案。 –

+0

然後你可以執行'textView.setText(entry.question)',同樣可以將'options []'中的答案設置爲RadioGroup。對於導航再次啓動相同的活動,但這次意圖做'putExtra Integer of ++ index'',你在'onCreate'中修改了當前活動實例 –