How to separate and identify objects within an Ajax request, with data coming from Servlet via Json?

Asked

Viewed 168 times

-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!");
        }

1 answer

0


If queryObject is in JSON format, will have a structure {key:value}. Then just pick up with:

var value = queryObject['key'];

If not, you can user the JSON.parse first:

var parsed = JSON.parse(objectToParse);
alert(parsed['key'])
  1. Make sure that you are sending a Servlet JSON as a response. Print it before sending and confirm.
  2. Make sure you’re getting a JSON in AJAX or turn it into one. Print it when you receive and confirm.

Your for it is not necessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.