S
You can get the values of the elements of your json with the method https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Object/values . This assuming that your json always has the same form and that the data you are interested in always come in the same position of the json. Thus, you can access the value of the element of position 1 (data), of element 0 (Table1), of element 1 (DataEnvioJson) of your received json. For example: var cantidad1 = 0;
var cantidad2 = 0;
var cantidadNull = 0;
$.getJSON('jsn.json', function(myJson) {
var datos = (Object.values(Object.values(Object.values(myJson)[1])[0])[1]);
for (var i = 0; i < datos.length; i++) {
if (Object.values(datos[i])[0] == 1) {
cantidad1++;
} else if (Object.values(datos[i])[0] == 2) {
cantidad2++;
} else {
cantidadNull++;
}
}
alert("Cantidad de 1'os: " + cantidad1 + "\nCantidad 2'os: " + cantidad2 + "\nCantidad null's: " + cantidadNull);
});
'json.json' is the json received. After this, you may have:var dt = [cantidad1, cantidad2, cantidadNull];
var jsonRecibido = {
"DatosEnvio": {},
"DatosEnvioJson": {
"Table1": {
"campos": [
"idCatSexo",
"idSysCPEmpleados"
],
"datos": [
{
"idCatSexo": 2,
"idSysCPEmpleados": 1
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 1
},
{
"idCatSexo": 1,
"idSysCPEmpleados": 52
},
{
"idCatSexo": 1,
"idSysCPEmpleados": 53
},
{
"idCatSexo": 1,
"idSysCPEmpleados": 54
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 55
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 56
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 57
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 58
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 59
},
{
"idCatSexo": 1,
"idSysCPEmpleados": 60
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 61
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 62
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 63
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 64
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 65
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 66
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 67
},
{
"idCatSexo": 1,
"idSysCPEmpleados": 68
},
{
"idCatSexo": 1,
"idSysCPEmpleados": 69
},
{
"idCatSexo": 1,
"idSysCPEmpleados": 70
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 71
},
{
"idCatSexo": 1,
"idSysCPEmpleados": 72
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 73
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 74
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 75
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 76
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 2027
},
{
"idCatSexo": 2,
"idSysCPEmpleados": 2028
}
]
},
"Table2": {
"campos": [
"bandera",
"mensaje"
],
"datos": [
{
"bandera": 0,
"mensaje": "Se ingresaron los datos correctamente"
}
]
}
},
"tablas": null,
"bandera": "0",
"mensaje": "--"
};
var cantidad1 = 0;
var cantidad2 = 0;
var cantidadNull = 0;
var datos = (Object.values(Object.values(Object.values(jsonRecibido)[1])[0])[1]);
for (var i = 0; i < datos.length; i++) {
if (Object.values(datos[i])[0] == 1) {
cantidad1++;
} else if (Object.values(datos[i])[0] == 2) {
cantidad2++;
} else {
cantidadNull++;
}
}
console.log("Cantidad de 1'os: " + cantidad1 + "\nCantidad 2'os: " + cantidad2 + "\nCantidad null's: " + cantidadNull);