简体   繁体   中英

How to parse a JSON in a JSON

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 !

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM