jaySON
jaySON

Reputation: 15

How to put multiple lists into one list?

Okay, so I need to put many different lists into one list using Java. Here is the important code.

String jsonText = buffer.toString(); // gets what the URL returns as JSON


                JSONObject obj = new JSONObject(jsonText); // using JSONObject to pass to a JSONArray to search for the JSON

                List<List<String>> allInfo = new ArrayList();// list to put all the returned information
                List<String> innerList = new ArrayList();
                JSONArray linemanques = obj.getJSONArray("linemanques"); //selects the array to read from


                for (int i = 0; i < linemanques.length(); i++) {
                    JSONObject questionParts = linemanques.getJSONObject(i);
                    quesnum = questionParts.getString("quesnum"); // all of questionParts.getString() are for getting the data in the JSONArray
                    questype = questionParts.getString("questype");
                    question = questionParts.getString("question");
                    ans1 = questionParts.getString("ans1");
                    ans2 = questionParts.getString("ans2");
                    ans3 = questionParts.getString("ans3");
                    ans4 = questionParts.getString("ans4");
                    correctans = questionParts.getString("correctans");
                    category = questionParts.getString("category");
                    notes = questionParts.getString("notes");
                    flag = questionParts.getString("flag");

                    innerList.add(quesnum);
                    innerList.add(questype);
                    innerList.add(question);
                    innerList.add(ans1);
                    innerList.add(ans2);
                    innerList.add(ans4);
                    innerList.add(correctans);
                    innerList.add(category);
                    innerList.add(notes);
                    innerList.add(flag);

                    allInfo.add(innerList);
                }

                return allInfo;

As shown above, I take a List called innerList and put it into another List, which is of type List called allInfo. In the for loop, I take the length of the JSONArray I'm using, and add the elements to innerList. Then, I take innerList and add it into allInfo, then return that.

By the way, here is the JSON.

{
    "error": false,
    "message": "Request successfully completed",
    "linemanques": [
        {
            "quesnum": 1,
            "questype": 1,
            "question": "This is question #1",
            "ans1": "This is answer 1",
            "ans2": "This is answer 2\r\n",
            "ans3": "This is answer 3\r\n\r\n\r\n",
            "ans4": "This is answer 4",
            "correctans": "ans4",
            "notes": "This is a note",
            "category": "2",
            "flag": "ans4"
        },
        {
            "quesnum": 2,
            "questype": 2,
            "question": "This is question #2",
            "ans1": "This is Q2 ans 1",
            "ans2": "This is Q2 ans2",
            "ans3": "This is Q2 ans3",
            "ans4": "This is Q2 ans4",
            "correctans": "ans2",
            "notes": "This is Q2 note 1",
            "category": "5",
            "flag": "ans2"
        },
        {
            "quesnum": 3,
            "questype": 6,
            "question": "gkjhgjkgkg",
            "ans1": "ghdfhdghfd",
            "ans2": "Tuituiyt",
            "ans3": "Tiuytui9",
            "ans4": "Tauitui.247",
            "correctans": "ans2",
            "notes": "Article iutuC",
            "category": "5",
            "flag": "ans2"
        },
        {
            "quesnum": 7,
            "questype": 2,
            "question": " how many",
            "ans1": "isi",
            "ans2": "thiis\\r\\n",
            "ans3": "yes",
            "ans4": "no",
            "correctans": "yes",
            "notes": "refer back to yes",
            "category": "0",
            "flag": "yes"
        },
        {
            "quesnum": 8,
            "questype": 2,
            "question": " how many",
            "ans1": "isi",
            "ans2": "thiis",
            "ans3": "yes",
            "ans4": "no",
            "correctans": "yes",
            "notes": "refer back to yes",
            "category": "0",
            "flag": "yes"
        },
        {
            "quesnum": 9,
            "questype": 2,
            "question": "How many apples can I eat in one day?",
            "ans1": "42 apples",
            "ans2": "6 apples",
            "ans3": "89 apples",
            "ans4": " 42 oranges",
            "correctans": "ans2",
            "notes": "try eating apples",
            "category": "8",
            "flag": "ans2"
        },
        {
            "quesnum": 10,
            "questype": 2,
            "question": " how many",
            "ans1": "isi",
            "ans2": "thiis\\r\\n\\r\\n\\r\\n",
            "ans3": "yes",
            "ans4": "no",
            "correctans": "yes",
            "notes": "refer back to yes",
            "category": "0",
            "flag": "yes"
        }
    ]
}

And this is the structure of the array I'm hoping for:

allInfo {
    innerList {
        JSONObject
        JSONObject
        JSONObject
    }
    innerList {
        JSONObject
        JSONObject
        JSONObject
    }
    innerList {
        JSONObject
        JSONObject
        JSONObject
    }
    innerList {
        JSONObject
        JSONObject
        JSONObject
    }
    innerList {
        JSONObject
        JSONObject
        JSONObject
    }
    innerList {
        JSONObject
        JSONObject
        JSONObject
    }
}

But instead, this is what I'm ending up with (i think):

allInfo {
    innerList {
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
        JSONObject
    }
}

It's supposed to make multiple lists in one list so that I can call access to it and set text in a TextView like this:

textView.setText(allInfo.get(2).get(4)); //example

Upvotes: 0

Views: 374

Answers (3)

Ilia Kuzmin
Ilia Kuzmin

Reputation: 165

If i understood correctly, you forget to clean innerList within for loop. You need to do:

innerList.clear()

Upvotes: -1

Noa
Noa

Reputation: 355

I think you do it in a hard way. you could use a library to deserializes your JSON like Jackson

So you could build pojo class out your keys

import com.fasterxml.jackson.databind.ObjectMapper;

class Pojo {
        String quesnum;
        int questype;
        String question;
        String ans1;
        String ans2;
        String ans3;
        String no;
        String correctans;
        String notes;
        int category;
        String flag;
}

then use Jackson to do the job for you by calling

List<Pojo> allInfo = mapper.readValue(json, new TypeReference<List<Pojo>>() {});

Upvotes: 0

hkim
hkim

Reputation: 68

I guess if you place the line List<String> innerList = new ArrayList(); within the for loop, it will properly initialize the innerList each time.

Upvotes: 4

Related Questions