I have in my "pck.json"
{
"Datos":
[
{
"Nombre":"Alekz"
},
{
"Nombre":"Andrea"
},
{
"Nombre":"Janny"
},
{
"Nombre":"Haytham"
}
],
"Datos2":
[
{
"Nombre":"Alekz"
},
{
"Nombre":"Andrea"
},
{
"Nombre":"Janny"
},
{
"Nombre":"Haytham"
}
],
"Datos3":
[
{
"Nombre":"Alekz"
},
{
"Nombre":"Andrea"
},
{
"Nombre":"Janny"
},
{
"Nombre":"Haytham"
}
]
}
In JQuery I have
jQuery(document).ready(function($) {
var obj = $.getJSON('pck.json',function(data){
var name = data.Datos;
var cont = 1;
for (var i = 0; i < name.length; i++) {
var object = name[i];
//alert(name.length);
for (property in object) {
var valor = object[property];
$(".unatabla").append("<tr><td>"+cont+"</td><td>"+valor+"</td></tr>");
cont+=1;
}
}
});
});
How can I count the elements within the JSON, that is, it gives me 3 as a result because in the JSON there are "Data","Data2", and "Data3". Please thanks.
Getting the length property of the "keys" object can be done with pure javascript (vanilla):
For references on this:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys