문제

After building an object array and posting it to a jsp page I'm having trouble figuring out how to parse the data.

var arr = [];           
for(var i=1; i <= $('#tableData tr').length; i++){
    var el = $("#tableData tr:nth-child("+i+")");
    var obj = {             
        id: el.find("td:nth-child(3)").text(),
        doc: el.find("td:nth-child(5)").text(),
        desc: el.find("td:nth-child(4)").text(),            
    };
    arr.push(obj);
}
$.ajax('controllers/savePrintOutDetail.jsp', {
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(arr),
    success: function(response){

    },
    error: function(){
        alert('error');
    }
})

I am aware I can retrieve the post data using getReader() but from there I don't know how to parse the array.

도움이 되었습니까?

해결책

I was able to parse the data using org.json

<%@ page import="org.json.JSONObject"%>
<%@ page import="org.json.JSONArray"%>

<%
  StringBuffer jb = new StringBuffer();
  String line = null;
  try {
    BufferedReader reader = request.getReader();
   while ((line = reader.readLine()) != null)
         jb.append(line);
   } catch (Exception e) { /*report an error*/ }
   JSONArray array = new JSONArray(jb.toString());
   for(int i=0;i<array.length();i++){
      JSONObject jsonObj = new JSONObject(array.get(i).toString());
      jsonObj.get("id")  // etc
    }
%>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top