【问题标题】:multiple JsonArrays and JSONobject for Ajax return success?Ajax 的多个 JsonArrays 和 JSONobject 返回成功?
【发布时间】:2016-10-27 23:11:30
【问题描述】:

我有 3 个 JSONArraysJSonobject 我想在 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 parsingjquery 变量中

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


    【解决方案1】:

    你可以这样试试。我不知道那些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);
    

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

    【讨论】:

    • 但我也有一个 JSONObject
    • 示例输出:[{},[],[],[]]
    • 在此处发布您的完整输出或创建新问题。我在这里看到的响应没有完成:(
    【解决方案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);
            }
        });
    }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 2016-01-30
      • 2012-07-13
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      相关资源
      最近更新 更多