0

I am retrieving the following strings into a json array. The codes are shown below and results printed:

public JSONArray selectGrades(ArrayList<String> dates) {
    JSONArray jArray = new JSONArray();
    ArrayList<String> grades= new ArrayList<String>();
    DbConnector connect = new DbConnector();
    for (int i = 0; i < dates.size(); i++) {
        String selectDetailsQuery = "SELECT grade FROM classes where exam_date = '" + dates.get(i)"'";
        //System.out.println(selectDetailsQuery);
        ResultSet rs = connect.executeQuery(selectDetailsQuery);
        try {

            while (rs.next()) {
                grades.add(rs.getString(1));
                jArray.put(grades);
                grades.clear();
            }

            System.out.println("JSON Array" + jArray);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //close all rs,stat,connection
    connect.close();
    return jArray;
}

JSON Printed out: JSON Array [[A,A,B,C,D]] JSON Array [[A,A,B,C,D,C,C,C,D,A]] JSON Array [[A,A,B,C,D,C,C,C,D,A,D,A,B,B,A]]

What it does is that it keep adding on to the JSON array that I have created upon looping. What I need is instead this result:

JSON Array [["A,A,B,C,D"]]
JSON Array [["A,A,B,C,D"],["C,C,C,D,A"]]
JSON Array [["A,A,B,C,D"],["C,C,C,D,A"],["D,A,B,B,A"]]

I am stuck at this for a few hours. WOuld appreciate any help given. Thank you.

1
  • You are printing jArray in one hit. You need to split it up and print it piece by piece. Commented Jul 30, 2011 at 14:42

3 Answers 3

1

Move the creation of your grades list into the for loop:

DbConnector connect = new DbConnector();
for (int i = 0; i < dates.size(); i++) {
    ArrayList<String> grades= new ArrayList<String>();

Also, I don't know where cmt is coming from, but your while loop needs to look more like this:

        while (rs.next()) {
            grades.add(rs.getString(1));
        }
        jArray.put(grades);

Update

For the output you're asking for, what you really need is something like this:

char separator = "";
StringBuilder sb = new StringBuilder();

while (rs.next()) {
    sb.append(separator).append(rs.getString(1));
    separator = ",";
}

jArray.put(new JSONArray(sb.toString()));

But be aware that the data structure you're creating is an array whose elements are arrays that only have one element, and the element they contain is a string ("A,B,C,..."). The Java equivalent would be a string[][]. There is a code smell here, and I think there's likely a better way to represent your data, but since I don't know how the data is going to be used I can't really advise on the best way to do it.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I managed to get the format I want thanks to your code. However, I found that they are not encoded in strings. JSON Array [["A,A,B,C,D"]] JSON Array [["A,A,B,C,D"],["C,C,C,D,A"]] JSON Array [["A,A,B,C,D"],["C,C,C,D,A"],["D,A,B,B,A"]]
@Dane: Are you aware that putting the values in strings is not a part of JSON encoding? It should look more like [["A","B","C"],["A",..],..}. But if you still want the output you posted, you'll need to create a comma-separated string to put into the jArray. The JSON Array won't do this for you because that's not the way a list of strings is supposed to be represented in JSON
Yes, I still want the output I posted. What do you mean by comma-separated string to put into the jArray? Could you post some examples please? thanks.
@Dane: I mean creating a string like "A,A,B,C,D", and then putting that into its own JSONArray, and putting that JSONArray into the overall JSONArray that you're trying to construct. It doesn't sound right to me, but you can see my edit for a way to go about doing this.
0

The output doesn't seem possible from the code listing you have given. More detail about the expected sequence of results from the ResultSet (rs)

Also, more detail about the JSONArray implementation. It is not a standard part of Java. Where did it come from?

Comments

0

Silly me. Ignore the previous answer. I missed that the arrayList is not being initialized before every while loop. So the existing contents are preserved and extended.

2 Comments

You should edit or delete your previous answer instead of creating a new answer for this comment.
@StriplingWarrior Point taken onboard. I'm new to this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.