Essentially, my goal is to pass a Java String array to a JavaScript array, then pass this array via JSON to a JSP page, where I can parse it as a Java array.
I attempted the following:
JSONArray arr = new JSONArray();
JSONObject tmp;
for(int i = 0; i < invoiceid.length; i++) {
tmp = new JSONObject();
tmp.put("invoiceid", invoiceid[i]);
arr.add(tmp);
}
Next, here is the JavaScript code on the same page:
var invoiceid = JSON.stringify(<%=arr%>);`
$.ajax({
type: 'GET',
url: 'crudsettlement.jsp',
data: {
Winvoiceid: invoiceid
},
async: false,
dataType: 'json',
success: function(json) {
$("#msg").val(json.msg);
}
});
In the page crudsettlement.jsp, I attempted the following:
JSONObject jObj = new JSONObject(request.getParameter("mydata"));
However, I encountered the error: "incompatible types: String cannot be converted to Map."
Do you have any ideas on how to convert the JSON array to a Java array? I am using json-simple.