-2
How do I separate the objects inside the Ajax
? The Servlet
sends a list of objects to Ajax
, but I can’t separate them inside the Ajax
to be able to pick up an attribute from a specific object, for example.
SERVLET
String op = request.getParameter("option");
if(op.equals("Car")){
try{
JSONArray jsonArray = new JSONArray();
JSONObject responseObj = new JSONObject();
CarDAO d = new CarDAO();
List<Car> procedimentos = d.listarCar();
for(Car obj : procedimentos){
JSONObject js = new JSONObject();
double mst = obj.getValor();
String data = obj.getData();
String nome = obj.getNome();
double avg = obj.getNome();
if(nome.contains("KAL_69KV")){
js.put("jsonKAL_69KV_percent",avg);
js.put("jsonKAL_69KV", mst);
js.put("json_KAL_69KV_Nome", ponto);
js.put("json_KAL_69KV_Data", data);
jsonArray.put(js);
}else if(nome.contains("KCM_23KV")){
String ponto = "KCM 23KV - CAMPO BOM 23KV";
js.put("jsonKAL_69KV_percent",avg);
js.put("jsonKCM_23", mst);
js.put("json_KCM_23_Nome",ponto);
js.put("json_KCM_23_Data", data);
jsonArray.put(js);
}
}
responseObj.put("jsonArray", jsonArray);
out.print(responseObj);
}catch (JSONException e){
e.printStackTrace();
}
...
...
Ajax
$j.ajax({
data: {
'option': "Car"
},
dataType: 'json',
url: './GetDados',
type: 'POST',
success: function(data) {
queryObject = eval('(' + JSON.stringify(data) + ')');
queryObjectLen = queryObject.jsonArray.length;
for (var i = 0; i < queryObjectLen; i++) {
//----------------------------------------------------
get data from object 1.name
get data from object 1.age
get data from object 1.anotherValue
//----------------------------------------------------
//----------------------------------------------------
get data from object 2.name
get data from object 2.age
get data from object 2.anotherValue
//----------------------------------------------------
//----------------------------------------------------
get data from object 3.name
get data from object 3.age
get data from object 3.anotherValue
//----------------------------------------------------
.
.
.
}
if (queryObjectLen === 0) {
alert("No data!");
}