Problem when creating a JSON from Servlet

0

Good, I want to go from a Servlet to a JavaScript a Json with the following data. A dealer has several orders assigned, so I want to pass the id and the position of the delivery person, along with the id and address of the orders assigned to him.

MyServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    Consulta cons, cons1;
    JSONObject json = new JSONObject();
    JSONArray  resultado = new JSONArray();
    JSONObject repartidor;
    JSONArray pedidos = new JSONArray();
    JSONObject pedido;

    int idRep, idPed;
    double repLat, repLng;
    String pedDireccion;

    try {
        repartidor = new JSONObject();
        pedido = new JSONObject();
        cons = new Consulta("*", "repartos r, pedidos p");
        cons1 = new Consulta("*", "usuarios");

        ResultSet rs1 = cons1.recuperar("estado <> 'INACTIVO'");
        while (rs1.next()){
            idRep = rs1.getInt("id");
            repLat = rs1.getDouble("posLat");
            repLng = rs1.getDouble("posLng");

            repartidor.put("idRepartidor", idRep);
            repartidor.put("latRepartidor", repLat);
            repartidor.put("lngRepartidor", repLng);

            ResultSet rs = cons.recuperar("idRepartidor = " + idRep + " and r.idPedido = p.id and p.id NOT IN (SELECT id FROM pedidos WHERE estado = 'ENTREGADO' OR estado = 'NO ENTREGADO' OR estado = 'CANCELADO')");
            while (rs.next()){
                idPed = rs.getInt("idPedido");
                pedDireccion = rs.getString("direccion");

                pedido.put("idPedido", idPed);
                pedido.put("direccionPedido", pedDireccion);

                pedidos.put(pedido);
            }
            repartidor.put("Pedidos", pedidos);
            resultado.put(repartidor);
        }
        json.put("Resultado", resultado);

        response.setContentType("application/json");
        response.getWriter().write(json.toString());
    }        
    catch (Exception e) {
    }
}

By doing a console.log() from the JavaScript , it returns this:

Object
    Resultado: Array[3]
        0: Object
            Pedidos: Array[3] // todos los datos de los pedios
            idRepartidor: 15
            latRepartidor: 0
            lngRepartidor: 0
        1: Object
            Pedidos: Array[3] // todos los datos de los pedios
            idRepartidor: 15
            latRepartidor: 0
            lngRepartidor: 0
        2: Object
            Pedidos: Array[3] // todos los datos de los pedios
            idRepartidor: 15
            latRepartidor: 0
            lngRepartidor: 0

He brings me the same delivery guy. In the bd I have 3 distributors with status ' ACTIVE ' and with assigned orders but it does not bring them to me.

UPDATE

In the bd in the table deals I have these records:

idRepartidor  -  idPedido
    11              31
    11              32
    12              33

It brings me to the dealer 11 the three orders (31,32,33) and for the rest of the delivery people too.

    
asked by Juan Manuel 26.01.2017 в 16:00
source

1 answer

3

You have to make the new () of the JSONObject objects inside the loops of the queries, otherwise you are always writing on the same java object instances.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    Consulta cons, cons1;
    JSONObject json = new JSONObject();
    JSONArray  resultado = new JSONArray();
    JSONObject repartidor;
    JSONArray pedidos = new JSONArray();
    JSONObject pedido;

    int idRep, idPed;
    double repLat, repLng;
    String pedDireccion;

    try {
        cons = new Consulta("*", "repartos r, pedidos p");
        cons1 = new Consulta("*", "usuarios");

        ResultSet rs1 = cons1.recuperar("estado <> 'INACTIVO'");
        while (rs1.next()){
            repartidor = new JSONObject();
            idRep = rs1.getInt("id");
            repLat = rs1.getDouble("posLat");
            repLng = rs1.getDouble("posLng");

            repartidor.put("idRepartidor", idRep);
            repartidor.put("latRepartidor", repLat);
            repartidor.put("lngRepartidor", repLng);
            pedidos = new JSONArray();

            ResultSet rs = cons.recuperar("idRepartidor = " + idRep + " and r.idPedido = p.id and p.id NOT IN (SELECT id FROM pedidos WHERE estado = 'ENTREGADO' OR estado = 'NO ENTREGADO' OR estado = 'CANCELADO')");
            while (rs.next()){

                pedido = new JSONObject();
                idPed = rs.getInt("idPedido");
                pedDireccion = rs.getString("direccion");

                pedido.put("idPedido", idPed);
                pedido.put("direccionPedido", pedDireccion);

                pedidos.put(pedido);
            }
            repartidor.put("Pedidos", pedidos);
            resultado.put(repartidor);
        }
        json.put("Resultado", resultado);

        response.setContentType("application/json");
        response.getWriter().write(json.toString());
    }        
    catch (Exception e) {
    }
}
    
answered by 26.01.2017 / 16:26
source