简体   繁体   English

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

[英]multiple JsonArrays and JSONobject for Ajax return success?

I have 3 JSONArrays and JSonobject that I want to return as success response in ajax . 我有3个JSONArraysJSonobject我想作为ajax success response返回。

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 . 我不知道如何在ajax - jquery获取数据。 I tried to run the program with one JSONArray and it perfectly works but 我试图使用一个JSONArray运行该程序,但效果很好,但是

i dont know how to create multiple arrays and a object and parsing them into jquery variables in return success of ajax. 我不知道如何创建多个arrays and a object and parsingjquery变量,以return success of ajax.

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

I am currently getting this reults, how do i get the data in jquery 我目前正在获得此结果,如何获取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)); 这是我通过使用console.log(JSON.stringify(data));在控制台console.log(JSON.stringify(data));其打印出来时得到的结果console.log(JSON.stringify(data)); but when i try to get the variable/data console.log("HELLO" +print[0].ByAgeGroupSexTable[0].location); 但是当我尝试获取变量/数据console.log("HELLO" +print[0].ByAgeGroupSexTable[0].location); this was the error Cannot read property '0' of undefined 这是error Cannot read property '0' of undefined

This is my JS code 这是我的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);
        }
    });
}));

You could try like this. 您可以尝试这样。 I don't know what kind of Objects have been pushed in those arraylist. 我不知道在这些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 : 输出1:

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

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 输出2

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

This line 这条线

var print = JSON.stringify(data);     //just remove JSON.stringify() 

Converts your object to a string so you can not access it. 将您的对象转换为字符串,以便您无法访问它。 You can easily access the object now 您现在可以轻松访问对象

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM