我正在我的 javascript 中构建一个 JSONObject,然后使用以下代码将其作为字符串发送到我的 servlet:
insertDtls = function() {
var jsonObj = [];
jsonObj.push({location: this.location()});
jsonObj.push({value: this.value()});
jsonObj.push({coverage: this.coverage()});
jsonObj.push({validPeriod: this.collateralValidPer()});
jsonObj.push({description: this.description()});
var b = JSON.stringify(jsonObj);
console.log(b.toString());
$.ajax({
url:"/HDSWFHub/AppProxy",
type: 'GET',
data: $.extend({WrJOB: "insertDtls", mainData: b}, tJS.getCommonPostData()),
dataType: "json",
success: function(responseText, status, xhr){
updateViewModel(responseText);
},
error: function(jqXHR, textStatus, error){
tJS.manageError(jqXHR);
}
});
},
字符串看起来像:
[{"location":"Boston"},{"value":"5"},{"coverage":"15"},{"validPeriod":"08/05/2013"},{"description":"test description"}]
并且 servlet 可以毫无问题地接收它。
然后我在我的servlet中得到这个:
String step = request.getParameter("mainData");
JSONObject jsonObj = new JSONObject();
final JSONObject obj = new JSONObject();
System.out.println(step);
try {
obj.put("viewModel", "index");
obj.put("WrSESSIONTICKET", sessionTicket);
response.getWriter().print(obj.toString());
} catch (final Exception e) {
logException(request, response, e, true);
}
我正在尝试将 JSON 字符串转换回 servlet 中的对象,以便能够循环遍历这些项目或获取所需的项目。我正在使用的图书馆是org.json
我累了:
JSONObject jsonObj = new JSONObject(step);
没有任何成功。刚刚收到此错误:
Unhandled exception type JSONException
我不知道发生了什么。也许我已经太累了。我确定我错过了一些非常小的东西,但我无法发现它。
我知道它已经被问了数百次了。我知道我会得到大量的反对票,但我无法找到我的问题的答案。