我想在JSONArrays
中以JSonobject
的身份返回3 success response
和ajax
。
我创建了
out.print(jarrayTable);
out.print(jarrayChartTotalMF);
out.print(jarrByAgeGroup);
out.print(objTotal);
我不知道如何在ajax - jquery
中获取数据。我尝试用一个JSONArray
来运行程序,它完全有效,但是
我不知道如何在arrays and a object and parsing
jquery
变量中创建多个return success of ajax.
个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...
我目前得到了这些结果,我如何获取jquery中的数据
console.log(JSON.stringify(data));
这是我使用console.log("HELLO" +print[0].ByAgeGroupSexTable[0].location);
在控制台上打印出来的内容,但是当我尝试获取变量/数据error
时,这就是Cannot read property '0' of undefined
$("#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);
}
});
}));
这是我的JS代码
ng-controller="mainController as ctrl"
答案 0 :(得分:3)
你可以这样试试。我不知道在那些arraylist中推送了什么样的对象。
ArrayList<Object> allList = new ArrayList<>();
ArrayList<Object> jarrayTable = new ArrayList<Object>();
ArrayList<Object> jarrayChartTotalMF = new ArrayList<Object>();
ArrayList<Object> jarrByAgeGroup = new ArrayList<Object>();
JsonObject objTotal= new JsonObject();
allList.add(objTotal);
allList.add(jarrayTable);
allList.add(jarrayChartTotalMF);
allList.add(jarrByAgeGroup);
String allJSON = new Gson().toJson(allList);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
se.getWriter().write(allJSON);
output1:
[{},[],[],[]]
或
HashMap<String, Object> allList = new HashMap();
ArrayList<Object> jarrayTable = new ArrayList<Object>();
ArrayList<Object> jarrayChartTotalMF = new ArrayList<Object>();
ArrayList<Object> jarrByAgeGroup = new ArrayList<Object>();
JsonObject objTotal= new JsonObject();
allList.put("obj", objTotal);
allList.put("arr1",jarrayTable);
allList.put("arr2",jarrayChartTotalMF);
allList.put("arr3",jarrByAgeGroup);
String allJSON = new Gson().toJson(allList);
OUTPUT2
{"obj":{},"arr2":[],"arr1":[],"arr3":[]}
答案 1 :(得分:2)
这一行
var print = JSON.stringify(data); //just remove JSON.stringify()
将对象转换为字符串,以便您无法访问它。您现在可以轻松访问该对象
$("#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 = data;
console.log("HELLO" +print[0].ByAgeGroupSexTable[0].location)
}, error: function (XMLHttpRequest, textStatus, exception) {
alert(XMLHttpRequest.responseText);
}
});
}));