当我尝试这个时:
JSONObject xyz = new JSONObject();
JSONArray abc = new JSONArray();
while(rs.next())
{
String a = rs.getString("enrollno");
String b = rs.getString("name");
xyz.put("no" , a);
xyz.put("name" ,b);
abc.put(xyz);
System.out.println(abc.toString());
}
输出如:
[{"no":"IU121","name":"Heer"}]
[{"no":"IU12410500","name":"Ravi"},{"no":"IU12410500","name":"Ravi"}]
[{"no":"IU1241050050","name":"Rax"},{"no":"IU1241050050","name":"Rax"},{"no":"IU1241050050","name":"Rax"}]
但我希望输出如下:
[{"no":"IU121","name":"Heer"},{"no":"IU12410500","name":"Ravi"},{"no":"IU1241050050","name":"Rax"}]
答案 0 :(得分:1)
试试这个
while(rs.next())
{
String a = rs.getString("enrollno");
String b = rs.getString("name");
JSONObject xyz = new JSONObject();
xyz.put("no" , a);
xyz.put("name" ,b);
abc.put(xyz);
System.out.println(abc.toString());
}