Print incoming json data in jquery post

Asked

Viewed 205 times

3

I am with the following doubt, I am making a freight calculation system the system is getting a json response in the following pattern.

{"cServico":
    [{"Codigo":"40010","Valor":"36,90","PrazoEntrega":"3","ValorSemAdicionais":"36,90","ValorMaoPropria"
    :"0,00","ValorAvisoRecebimento":"0,00","ValorValorDeclarado":"0,00","EntregaDomiciliar":"S","EntregaSabado"
    :"S","Erro":"0","MsgErro":{}},{"Codigo":"41106","Valor":"17,80","PrazoEntrega":"6","ValorSemAdicionais"
    :"17,80","ValorMaoPropria":"0,00","ValorAvisoRecebimento":"0,00","ValorValorDeclarado":"0,00","EntregaDomiciliar"
    :"S","EntregaSabado":"N","Erro":"0","MsgErro":{}}
]}

how can I be displaying this data.

$('body').append(data);

with this code it shows the entire array more accurate just a few data and I’m not getting, from now thank you.

  • You made this work?

1 answer

5


This JSON has to be converted to Object first. You can do this using JSON.parse:

var dados = JSON.parse(data);

then it depends on how you want to use the data you have in this property/key array cServico.

I leave an example with a table:

var dados = JSON.parse(data);
var table = document.createElement('table');

// table head
var tr = document.createElement('tr');
Object.keys(dados.cServico[0]).forEach(function (chave) {
    var th = document.createElement('th');
    var tdConteudo = document.createElement('td');
    th.innerHTML = chave;
    tr.appendChild(th);
});
table.appendChild(tr);

// conteudo tabela
dados.cServico.forEach(function (el) {
    var tr = document.createElement('tr');
    Object.keys(el).forEach(function (chave) {
        var tdConteudo = document.createElement('td');
        tdConteudo.innerHTML = el[chave];
        tr.appendChild(tdConteudo);
    })
    table.appendChild(tr);
});
document.body.appendChild(table);

jsFiddle: http://jsfiddle.net/kxL2h17g/

  • 1

    I fixed and showed the data now I will try to format, since thank you very much

  • 1

    @Claytoneduardomergulhão if my answer solved the problem you can mark as accepted.

Browser other questions tagged

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