Java 从AJAX检索ArrayList Json对象
我有以下从数据库检索数据的代码:Java 从AJAX检索ArrayList Json对象,java,jquery,json,ajax,Java,Jquery,Json,Ajax,我有以下从数据库检索数据的代码: List<LocationBean> Locations = new ArrayList<LocationBean>(); /* * The following logic iterates through the database entries * */ for(int i = 1; i<=10; ++i){ String quer
List<LocationBean> Locations = new ArrayList<LocationBean>();
/*
* The following logic iterates through the database entries
* */
for(int i = 1; i<=10; ++i){
String query ="SELECT * FROM Energyinfo.Info WHERE id_number = " +i;
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
int id = rs.getInt("id_number");
int kw = rs.getInt("kilowatts_used");
double lat = rs.getDouble("Latitude");
double lon = rs.getDouble("Longitude");
LocationBean temp = new LocationBean(id, kw, lat, lon);
Locations.add(temp);
}}
String jsonData = new Gson().toJson(Locations);
System.out.println(jsonData);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonData);
List Locations=new ArrayList();
/*
*以下逻辑遍历数据库条目
* */
对于(int i=1;iI应该提到这段代码在Servlet中的一个doGet方法中如果你只想发送json
为什么不创建一个json
?你能给出一个例子吗?PS我如何检索前端的每个json对象?回答你的最后一个问题:用JavaScript函数解析它们。