Concat Same Name将不同的值JSON对象转换为唯一的

时间:2016-03-05 13:22:37

标签: java arrays json

当我尝试这个时:

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"}]

1 个答案:

答案 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());
}