Separate an array of JSON into columns and rows

1

I'm getting an array of JSON in the back end like this:

{"idDistrito": "23",
    "nome": "rere",
    "codigoDne": "154545",
    "cns": "565665",
    "entidade": {
        "idEntidade": "1",
        "nome": "Entidade 01"
    },        
    "emissaoProfissional": "07/11/2017"

}

I would like to know how do I separate the attributes of the variables to set up a table where the attributes would be column and the variables would be rows.

    
asked by anonymous 11.12.2017 / 12:37

1 answer

2

You can use the .each() method, it is designed to make DOM loop constructs concise and less prone to errors. When called iterate over the DOM elements that are part of the jQuery object. Each time the callback is executed, the current loop iteration is passed, starting from 0.

var html_ = "";
$.each( msg, function( key, value ) {
    html_ += "<tr>";
    html_ += "<td>"+value.idDistrito+"</td>";
    html_ += "<td>"+value.nome+"</td>";
    html_ += "<td>"+value.campox+"</td>";
    html_ += "<td>"+value.campoy+"</td>";
    html_ += "</td></tr>";
    // ou ao inves da variavel html pode usar o 
    $("#tabela").last().append("<tr><td>"+value.idDistrito+"</td><td>"+value.nome+"</td><td>"+value.campox+"</td>td>"+value.campoy+"</td></td></tr>");
});
$("#tabela").html(html_);
    
11.12.2017 / 12:57