gpt4 book ai didi

javascript - Ajax 的多个 JsonArrays 和 JSONobject 返回成功?

转载 作者:行者123 更新时间:2023-11-30 08:31:00 25 4
gpt4 key购买 nike

我有 3 JSONArraysJSonobject我想以 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. 中的变量

我也试过这样做,但我不知道如何在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);
}
});
}));

最佳答案

你可以这样试试。我不知道在那些数组列表中推送了什么样的对象。

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);

输出1:

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

或者

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);

输出2

{"obj":{},"arr2":[],"arr1":[],"arr3":[]}

关于javascript - Ajax 的多个 JsonArrays 和 JSONobject 返回成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38028526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com