所以這裏是
你jQuery來的數據推送到可變
$.ajax({
url : "NameServlet",
dataType : 'json',
error : function() {
alert("Error Occured");
},
success : function(data) {
var receivedData = [];
$.each(data.jsonArray, function(index) {
$.each(data.jsonArray[index], function(key, value) {
var point = [];
point.push(key);
point.push(value);
receivedData.push(point);
});
});
}
});
答案
在此之後,你需要servlet來獲得JSON對象
的Servlet會像
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NameServlet extends HttpServlet {
int []sampleData=null;
//sampleData= here you can get data from database
//writing data to json
response.setContentType("application/json;charset=utf-8");
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
JSONObject member = new JSONObject();
member.put("arrayData", sampleData);
array.add(member);
json.put("jsonArray", array);
PrintWriter pw = response.getWriter();
pw.print(json.toString());
pw.close();
}
希望這有助於
使用'console'對象的JavaScript的'log'方法來查看結構你稱之爲「數據」的對象。然後你可以決定如何處理你的成功函數中的'data'對象。 – steampowered