我正在从ajax调用java发送一个json对象。
$.ajax({
cache: false,
url: 'AddPPCheques.ws',
type: "POST",
contentType: "application/json",
dataType: "json",
data: "chequesList=" + JSON.stringify(myJson),
success: function (data) {
}
}
);
浏览器控制台 - >
chequesList=[{"Bank Code":"4234-322","Cheque No":"23432232","Amount":"432432","Commission":"427","Today":"2018-06-08"},{"Bank Code":"4234-112","Cheque No":"778787","Amount":"8986787","Commission":"2323","Today":"2018-06-08"}]
在java方面我试图从HttpServletRequest中读取它。
public ActionForward addCheques(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
JSONObject myResponse = new JSONObject ( request.getParameter("chequesList").toString() );
}
但是我得到一个NullPointerException。我在这做错了什么?以及如何阅读json对象的详细信息?
答案 0 :(得分:0)
原因是您将数据作为JSON
传递,但是您希望将其作为String并调用toString()
,但是您将获得null,因此将发生NullPointerException
请将您的ajax
代码更改为以下代码:
$.ajax({
cache: false,
url: 'AddPPCheques.ws',
type: "POST",
data: {chequesList:JSON.stringify(myJson)},
success: function (data) {
}
}
);