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.
jArrayin one hit. You need to split it up and print it piece by piece.