主要做的是从后台数据库查询记录并返回json给前端的ajax;
ajax如何遍历这一串 数据?求大神指导
{
"Product": [
{"proPic":"ccc","proID":1,"proName":"iPhone xs 64G","proPrice":5699},
{"proPic":"xx","proID":2,"proName":"xx","proPrice":333}
]
}
$.ajax({
type:"GET",
url:"http://localhost:8080/buyCar/Give",
success:function(data){ //成功的回调函数
var jsonobj=$.parseJSON(data);
/*这里不知道怎么写了*/
}
});
后台java文件输出json
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
Gson gson = new Gson();
PrintWriter out=response.getWriter();
String sql = "select * from productSimple";
try {
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()) {
product sp = new product();
sp.setProID(Integer.parseInt(rs.getString("productID")));
sp.setProName(rs.getString("productName"));
sp.setProPic(rs.getString("productPic"));
sp.setProPrice(Integer.parseInt(rs.getString("productPrice")));
jsonArray.add(gson.toJson(sp));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jsonObject.put("Product",jsonArray);
out.println(jsonObject);
DBUtil.release(conn, rs, pstm);
}