2016-06-25 58 views
1

我有3 JSONArraysJSonobject,我想返回success responseajax用於Ajax的多個JsonArray和JSONobject返回成功?

我創建

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

我目前得到這個reults,我如何在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);這是errorCannot 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); 
     } 
    }); 
})); 

回答

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

你可以試試這個。我不知道這些數據列表中推出了哪種對象。

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

,但我也有一個JSONObject的 – SCS

+0

輸出樣本:[{},[] [] [] –

+0

安置自己的完整輸出這裏,或創建新的問題。我能看到的迴應是沒有在這裏完成:( –