SCS SCS - 5 months ago 8
Javascript Question

multiple JsonArrays and JSONobject for Ajax return success?

I have 3

JSONArrays
and
JSonobject
that I want to return as
success response
in
ajax
.

I have created

out.print(jarrayTable);
out.print(jarrayChartTotalMF);
out.print(jarrByAgeGroup);
out.print(objTotal);


I dont know how to get the data in
ajax - jquery
. I tried to run the program with one
JSONArray
and it perfectly works but

i dont know how to create multiple
arrays and a object and parsing
them into
jquery
variables in
return success of ajax.


I also tried to do this, but i dont know how to parse the data in 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);





I am currently getting this reults, how do i get the data in 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...


this is what i get when i print it out on console by using
console.log(JSON.stringify(data));
but when i try to get the variable/data
console.log("HELLO" +print[0].ByAgeGroupSexTable[0].location);
this was the
error
Cannot read property '0' of undefined


This is my JS code

$("#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);
}
});
}));

Answer

You could try like this. I don't know what kind of Objects have been pushed in those 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 :

 [{},[],[],[]]

Or

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":[]}