Reputation: 152
i'm creating a mcq for a medical application and i'm trying to get some question from my databse with the different choice with this JSON :
{
"QCM": [{
"question": "Est-ce que Guillaume a pris?",
"id": "34",
"choix": ["Oui", "Non"]
}]
}
then i past my question string to a textview and if i have 2 choice, i create only 2 button, but only 1 button is create and in my button i have this string :
["Oui", "Non"]
So i don't understand because i create a second JSONArray loop for it ...
Here is my Java
try
{
JSONArray QCM = response.getJSONArray("QCM");
for (int i=0; i<QCM.length(); i++) {
JSONObject getQcmObject = QCM.getJSONObject(i);
String questionGet = getQcmObject.getString("question");
symptomesQuestions.setText(questionGet);
for (int x=0; x<QCM.length(); x++){
JSONObject getChoixObject = QCM.getJSONObject(x);
String choiceGet = getChoixObject.getString("choix");
lesChoixButton.setText(choiceGet);
}
}
If someone can explain me how to do it, i want to learn ! can't find any exemple for this kind of request. Thanks folks !
Upvotes: 0
Views: 63
Reputation: 452
you use wrong parser ,change it like this:
JSONArray QCM = response.getJSONArray("QCM");
for (int i = 0; i < QCM.length(); i++) {
JSONObject getQcmObject = QCM.getJSONObject(i);
String questionGet = getQcmObject.getString("question");
symptomesQuestions.setText(questionGet);
JSONArray choiceGet = getChoixObject.getJSONArray("choix");
lesChoixButton1.setText(choiceGet.getString(0));
lesChoixButton2.setText(choiceGet.getString(1));
}
use this site to create java pojo model from your json : http://www.jsonschema2pojo.org/
Upvotes: 2