【发布时间】:2016-10-27 23:11:30
【问题描述】:
我有 3 个 JSONArrays 和 JSonobject 我想在 ajax 中以 success response 的形式返回。
我已经创建了
out.print(jarrayTable);
out.print(jarrayChartTotalMF);
out.print(jarrByAgeGroup);
out.print(objTotal);
我不知道如何获取ajax - jquery 中的数据。我尝试用一个JSONArray 运行该程序,它运行良好,但是
我不知道如何在return success of ajax. 中创建多个arrays and a object and parsing 到jquery 变量中
我也尝试过这样做,但是我不知道如何在 jquery 中解析数据
String json1 = new Gson().toJson(jarrayTable);
String json2 = new Gson().toJson(objTotal);
String json3 = new Gson().toJson(jarrayChartTotalMF);
String json4 = new Gson().toJson(jarrByAgeGroup);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
String AllJson = "[" + json1 + "," + json2 + "," + json3 + "," + json4 + "]"; //Put both objects in an array of 2 elements
response.getWriter().write(AllJson);
我目前正在得到这个结果,我如何在 jquery 中获取数据
[{"ByAgeGroupSexTable":[{"BothSexes":42,"AgeGroup":"Under 1","ApprovedBy":"Geraldine Atayan","Male":25,"Female":17,"location":"Barangay 1","UploadedBy":"Shermaine Sy"},{"BothSexes":42,"AgeGroup":"Under 1","ApprovedBy":"Geraldine Atayan","Male":25,"Female":17,..."arrByAgeGroup":[{"arrByAgeGroupBothSexes":0,"arrByAgeGroupMale":25,"arrByAgeGroupFemale":17,"arrByAgeGrouplocation":"Barangay 1","arrByAgeGroupAgeGroup":"Under 1"},{"arrByAgeGroupBothSexes":0,"arrByAgeGroupMale":25,"arrByAgeGroupFemale":17,"arrByAgeGrouplocation":"Barangay...
这是我在控制台上使用console.log(JSON.stringify(data)); 打印出来的结果,但是当我尝试获取变量/数据console.log("HELLO" +print[0].ByAgeGroupSexTable[0].location); 这是error Cannot read property '0' of undefined
这是我的 JS 代码
$("#archived tbody").on("click", 'input[type="button"]', (function () {
var censusYear = $(this).closest("tr").find(".nr").text();
alert(censusYear);
var page = document.getElementById('page').value;
$.ajax({
url: "SetDataServlet",
type: 'POST',
dataType: "JSON",
data: {
censusYear: censusYear,
page: page
},
success: function (data) {
console.log(JSON.stringify(data));
var print = JSON.stringify(data);
console.log("HELLO" +print[0].ByAgeGroupSexTable[0].location);
...some other codess
}, error: function (XMLHttpRequest, textStatus, exception) {
alert(XMLHttpRequest.responseText);
}
});
}));
【问题讨论】:
标签: javascript jquery json ajax servlets