1

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.

1 Answer 1

1

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
    }
%>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.