function forSave(){
var table = document.getElementById("tableId");
var tbody = table.getElementsByTagName("tbody")[0];
var trs = tbody.getElementsByTagName("tr");
var trsArr = [];
var i = 0;
for (i = 0;i < trs.length;i++){
var obj = {};
var tds = trs[i].getElementsByTagName("td");
for (var j = 0;j < tds.length;j++){
var ss = tds[j].childNodes[0].value;
var ss1 =encodeURI(encodeURI( tds[j].childNodes[0].name));
obj[ss1] = ss;
}
trsArr.push(obj);
}
alert(JSON.stringify(trsArr));//将json对象转化为字符串
var bburl = "<%=request.getContextPath() %>/bbDeal";
alert(bburl);
$.ajax({
url:bburl,
async:false,
data:{"data":JSON.stringify(trsArr)},
dataType:"json",
type:'post'
});
}
后台
package com.genersoft.yjgl.bbhz.kcswtzfjh.bbInsert;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import com.genersoft.yjgl.bbhz.kcswtzfjh.data.Kcswtzfjh;
public class BbDealServlet extends HttpServlet{
private static Log log = LogFactory.getLog(BbDealServlet.class);
public void init(){
}
protected void doGet(HttpServletRequest req,HttpServletResponse rep) throws ServletException, IOException{
}
protected void doPost(HttpServletRequest req,HttpServletResponse rep) throws ServletException, IOException{
// JsonObject object=(JsonObject)new JsonParser().parse("data");
req.setCharacterEncoding("utf-8");
log.debug("------------------>doPost"+req.getParameter("data"));
try {
JSONArray jsonarray = new JSONArray(req.getParameter("data"));
int len = jsonarray.length();
List<Kcswtzfjh> list = new ArrayList<Kcswtzfjh>();
for (int i = 0;i <len;i++)
{
Kcswtzfjh kcswtzfjh = new Kcswtzfjh();
jsonarray.getJSONObject(0).get("depot_name");
kcswtzfjh.setDepot_name((String) jsonarray.getJSONObject(0).get("depot_name"));
list.add(kcswtzfjh);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}