How to view a JSON file with dynamic key/value?

Asked

Viewed 48 times

1

We have a JSON file that we will never know how many key/value will contain and need to display on the screen for the user, how to do this since I do not have the "key" example:

In the 1st. call ajax can come like this:

{
    "carros" : [
        {"fabricante":"fiat","modelo":"palio","ano":"2013","combustivel":"gasolina"},
        {"fabricante":"volkswagen","modelo":"fusca","ano":"1986","combustivel":"gasolina"},
        {"fabricante":"gm","modelo":"prisma","ano":"2011","combustivel":"gasolina"} 
    ]
}

In the 2nd. call ajax can come like this:

{
    "carros" : [
        {"fabricante":"fiat","teto_solar":"sim"},
        {"fabricante":"volkswagen","teto_solar":"sim"},
        {"fabricante":"gm","teto_solar":"sim"},
        {"fabricante":"ferrari","teto_solar":"sim"},
        {"fabricante":"gm","teto_solar":"sim"},
        {"fabricante":"fiat","teto_solar":"sim"}
    ]
}

In the 3rd. call ajax can come like this:

{
    "carros" : [
        {"kit_multimidia":"sim","modelo":"palio","ano":"2013","combustivel":"gasolina"},
        {"kit_multimidia":"não","modelo":"fusca","ano":"1986","combustivel":"gasolina"}
    ]
}

This is called Ajax: Note that within the $.each how do I display since I do not know the name of the "Chave" ?

$.ajax({
        url: url,
        type: "POST",
        data: JSON.stringify(model),
        contentType: "application/json; charset=utf-8",
        processData: false,
        cache: false,
        success: function (response) {
             $.each(response.listaDeCarros, function (index, item) {
                console.log("Campo 1" + item.Campo1);
                console.log("Campo 2" + item.Campo2);
                console.log("Campo 3" + item.Campo3);
             });             
        },
        error: function (response) {
        }
    });
};

1 answer

2


Example:

response = {
    "carros" : [
        {"fabricante":"fiat","teto_solar":"sim"},
        {"fabricante":"volkswagen","teto_solar":"sim"},
        {"fabricante":"gm","teto_solar":"sim"},
        {"fabricante":"ferrari","teto_solar":"sim"},
        {"fabricante":"gm","teto_solar":"sim"},
        {"fabricante":"fiat","teto_solar":"sim"}
    ]
}

response.carros.forEach(carroItem => {
    console.log(JSON.stringify(carroItem));
    Object.keys(carroItem).forEach((key, indice) => {
        console.log('indice: '+ indice +' - key: '+ key +' - Valor:'+ carroItem[key]);
    });
});

  • 1

    Leonardo would be more interesting to the author of the question and to other users who describe what the code does instead of just entering the code in the answer.

  • Good morning! @Leonardo Getulio thanks for the collaboration, but there’s still the question of how to catch the Valor since I have the Chave ?

  • Hi @hard123 made a change in the code, you can access using caroItem[key]

  • 1

    Okay! @Leonardo Getulio is just what I need !! I will adapt my needs ! Thanks !!

  • 1

    @Leandrade I will improve this question, I am new here, I am learning to answer and I am very happy to help. I’ll try to be as didactic as possible from now on.

Browser other questions tagged

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