javajsoup

How to split string and fill table - JAVA JSOUP


I 'm Trying split the text of this request: `

Document document = Jsoup .connect("https://web.servientrega.com/PortalServientrega/WebServicePortal/tracking/api/envio/2003159943/1/es") 
        .validateTLSCertificates(false) 
        .ignoreContentType(true) 
        .get(); System.out.println(document.text());

with the result I want to fill a table with the information that I got. image: Image with I want to do


Solution

  • The response that page is giving you is in Json format. You need to parse it before you can process it. I would suggest using Gson to parse this response. Currently the latest version of Gson is 2.10.1, which can be downloaded here.

    This is a working example that puts the response into a table:

    String[][] table;
    JsonParser parser = new JsonParser();
    JsonObject obj = parser.parse(document.text()).getAsJsonObject();
    JsonArray array = obj.get("movimientos").getAsJsonArray();
    table = new String[3][array.size()];
    for (int i = 0; i < array.size(); i++) {
        JsonObject element = array.get(i).getAsJsonObject();
        table[0][i] = element.get("fechaDatetime").getAsString();
        table[1][i] = element.get("movimiento").getAsString();
        table[2][i] = element.get("ubicacion").getAsString();
    }
    

    The resulting table looks like this:

    ------------------------------------------------------------------------------------------
    |    2018-04-04T18:09:16    |   Guia generada               |   Bogota (Cundinamarca)    |
    |    2018-04-05T01:37:00    |   Ingreso al centro logistico |   Bogota (Cundinamarca)    |
    |    2018-04-05T20:29:35    |   Salio a ciudad destino      |   Bogota (Cundinamarca)    |
    |    2018-04-06T23:52:59    |   Ingreso al centro logistico |   Barranquilla (Atlantico) |
    |    2018-04-09T07:50:38    |   En zona de distribucion     |   Barranquilla (Atlantico) |
    |    2018-04-09T10:17:36    |   Reportado entregado         |   Barranquilla (Atlantico) |
    |    2018-04-09T18:29:54    |   Entrega verificada          |   Barranquilla (Atlantico) |
    ------------------------------------------------------------------------------------------