Access the names of the fields in a JSON file

-1

I'm trying to make a dynamic table from a JSON file. The problem is that I want the headers of each column to be the name of the field. I mean, I have the JSON file:

"articulos": [
    {
        "nombre": "Camiseta",
        "talla": "XL",
        "descripcion": "100% Algodon"
    }]

Well, I need the first row of the table to be " nombre ", " talla " and " descripcion ". I have searched everywhere but I only find how to access the attributes. I appreciate the help in advance.

    
asked by AntonioMP87 07.04.2017 в 00:27
source

1 answer

0

you can go through the data in this way

<script>
var miJSON={
    "A1":{"valor":"100", "color":"azul", "caracteristica":{"tipo":"S1"}},
    "A2":{"valor":"110", "color":"rojo", "caracteristica":{"tipo":"S2"}},
    "A3":{"valor":"120", "color":"negro", "caracteristica":{"tipo":"S3"}},
    "A4":{"valor":"90", "color":"verde", "caracteristica":{"tipo":"S4"}},
}

$(document).ready(function(){
    var table="<table><tr><td>Id Registro</td><td>Valor</td><td>Color</td><td>Caracteritica</td></tr>";
    $.each(miJSON, function(i,item){
        table+="<tr><td>"+i+"</td><td>"+miJSON[i].valor+"</td><td>"+miJSON[i].color+"</td><td>"+miJSON[i].caracteristica.tipo+"</td></tr>";
    });
   table+="</table>";
   $('#anyDiv').append(table);
});
</script>

I would do this directly to generate the table

    
answered by 07.04.2017 в 00:40