2016-06-25 6 views
1

Ich habe 3 JSONArrays und JSonobject, die ich als success response in ajax zurückgeben möchte.mehrere JsonArrays und JSONObject für Ajax return success?

Ich habe

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

Ich weiß nicht, wie die Daten in ajax - jquery zu erhalten. Ich habe versucht, das Programm mit einem JSONArray und es funktioniert perfekt, aber

ich weiß nicht, laufen, wie mehrere erstellen arrays and a object and parsing sie in jquery Variablen in return success of ajax.

Ich habe auch versucht, dies zu tun, aber ich weiß nicht, wie zu analysieren die Daten 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); 

ich bin derzeit immer diese reults, wie ich die Daten in jquery erhalten Sie

[{"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... 

das ist, was ich bekomme, wenn ich es von console.log(JSON.stringify(data)); mit auf Konsole aus, aber wenn ich versuche, die Variable/Daten zu erhalten console.log("HELLO" +print[0].ByAgeGroupSexTable[0].location); dies war die errorCannot read property '0' of undefined

Das ist mein 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); 
     } 
    }); 
})); 

Antwort

2

Diese Linie

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

Konvertiert Ihr Objekt in eine Zeichenfolge, sodass Sie nicht darauf zugreifen können. Sie können ganz einfach auf das Objekt zugreifen jetzt

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

Sie könnten so versuchen. Ich weiß nicht, welche Art von Objekten in dieser Arraylist geschoben wurden.

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:

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

Oder

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

, aber ich habe auch eine JSONObject – SCS

+0

Beispielausgabe: [{} [], [], []] –

+0

Post vollständige Ausgabe hier oder eine neue Frage erstellen. Was ich die Antwort sehen kann, ist hier nicht abgeschlossen :( –