How can I parse nested array inside JSON object using Android Volley

I want to parse below JSON format,
Can anybody please advise a way to do it? Thanks.

JSON format, I want to parse

{
"questions": [
{
"question": "What is the scientific name of a butterfly?",
"answers": [
"Apis",
"Coleoptera",
"Formicidae",
"Rhopalocera"
],
"correctIndex": 3
},
{
"question": "How hot is the surface of the sun?",
"answers": [
"1,233 K",
"5,778 K",
"12,130 K",
"101,300 K"
],
"correctIndex": 1
},
{
"question": "Who are the actors in The Internship?",
"answers": [
"Ben Stiller, Jonah Hill",
"Courteney Cox, Matt LeBlanc",
"Kaley Cuoco, Jim Parsons",
"Vince Vaughn, Owen Wilson"
],
"correctIndex": 3
}
]
}

I am trying to get it done by the below codes, It’s not completed, I can parse the first array and object inside it.

public List getGeneralQuestion(final AnswerListAsyncResponse callBack){

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
            URL, null, response -> {

        try {
            JSONArray jsonArray = response.getJSONArray("questions");

            for (int i = 0; i < jsonArray.length(); i++){

                JSONObject jsonObject = jsonArray.getJSONObject(i);

                String question = jsonObject.getString("question");
                int correctAnswer = jsonObject.getInt("correctIndex");

                GeneralQuestions generalQuestions = new GeneralQuestions(question, correctAnswer);
                generalQuestionsArrayList.add(generalQuestions);


                JSONArray jsonAnsArray = jsonObject.getJSONArray("answers");

                for (int j = 0; j < jsonAnsArray.length(); j++){

                    JSONArray jsonArrayList = jsonAnsArray.getJSONArray(j);

                    String answerList = jsonArrayList.getString();///////


                    GeneralQuestions generalAnswer = new GeneralQuestions(answerList);

                    generalAnsArrayList.add(generalAnswer);
                }



            }

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

  1. Create your response model class. You can do it with an online tool or android studio plugins.
    You can find a good online tool here .And about android studio plugins , Go to File -> Settings -> Plugins and search for JSON to POJO and install a good plugin and restart android studio in order to use it .

  2. stop going through each and every JSON node since this is a simple JSON response. Use GSON for easy conversions. add GSON dependency to your app level Build.gradle file and start using GSON to convert your JSON response to a List of model class objects. It’s simple and easy. You can find a good tutorial here.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply