从mysql获取数据后,我需要将数据导出为JSON,格式如下:
{"Thong tin":[{"Ngay":"2013-06-18","Tinh":"An Giang"},{"Ngay":"2013-06-17","Tinh":"Bình Dương"},{"Ngay":"2013-06-16","Tinh":"Bạc Liêu"}]}
但我得到的,就像
{"Thong tin":[{"Ngay":"2013-06-16","Tinh":"Bạc Liêu"},{"Ngay":"2013-06-16","Tinh":"Bạc Liêu"},{"Ngay":"2013-06-16","Tinh":"Bạc Liêu"}]}
你能帮我解决这个错误吗?
我的代码:
acc = new access();
rs2 = acc.query("select province_Name, date_Expired from thong_tin_khach_hang");
List<String> province_Name = new ArrayList<String>();
List<String> date_Expired = new ArrayList<String>();
try {
while (rs2.next()) {
province_Name.add(rs2.getString(1));
date_Expired.add(rs2.getString(2));
}
} catch (SQLException e) {
try {
acc.con.close();
} catch (SQLException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
JSONObject obj = new JSONObject();
JSONArray jarray = new JSONArray();
try {
JSONObject ob = new JSONObject();
for (int i = 0; i < province_Name.size(); i++) {
ob=new JSONObject();
ob.put("Tinh", province_Name.get(i));
ob.put("Ngay", date_Expired.get(i));
jarray.add(ob);
}
obj.put("Thong tin", jarray);
} catch (JSONException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
out.print(obj);