Print json data received in jquery post

Posted on

Question :

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


How can I be displaying this data.


With this code it shows the most accurate array of just some data and I can not thank you in advance.


Answer :

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

var dados = JSON.parse(data);

Then it depends on how you want to use the data that you have in this property array / key 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;

// 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];

jsFiddle: link


Leave a Reply

Your email address will not be published. Required fields are marked *