Obtaining values of a json in js

0

I need to save the data I'm receiving from a json for this occasion I need to save all the data I'm getting in different variables, that is, by data I need to create a new variable that contains it ... I have more or less the idea. I suppose...

var r = "json";
var datos = (Object.values(Object.values(Object.values(r)[1])[0])[1]);
for (var i = 0; i < datos.length; i++) {


  if (Object.values(datos[i])[0] == 1) {
    // guardar en variables de está posición 
  } else if (Object.values(datos[i])[0] == 2) {
    // guardar en variables de está posición 
  } else {
    cantidadNull++;
  }
}

{
  "DatosEnvio": {},
  "DatosEnvioJson": {
    "Table1": {
      "campos": ["Column1", "total"],
      "datos": [{
        "Column1": 1956,
        "total": 1
      }, {
        "Column1": 1963,
        "total": 2
      }, {
        "Column1": 1966,
        "total": 1
      }, {
        "Column1": 1967,
        "total": 2
      }, {
        "Column1": 1968,
        "total": 1
      }, {
        "Column1": 1969,
        "total": 3
      }, {
        "Column1": 1970,
        "total": 2
      }, {
        "Column1": 1971,
        "total": 2
      }, {
        "Column1": 1972,
        "total": 3
      }, {
        "Column1": 1973,
        "total": 3
      }, {
        "Column1": 1974,
        "total": 1
      }, {
        "Column1": 1975,
        "total": 2
      }, {
        "Column1": 1976,
        "total": 1
      }, {
        "Column1": 1977,
        "total": 8
      }, {
        "Column1": 1978,
        "total": 2
      }, {
        "Column1": 1979,
        "total": 8
      }, {
        "Column1": 1980,
        "total": 6
      }, {
        "Column1": 1981,
        "total": 9
      }, {
        "Column1": 1982,
        "total": 8
      }, {
        "Column1": 1983,
        "total": 3
      }, {
        "Column1": 1984,
        "total": 3
      }, {
        "Column1": 1985,
        "total": 7
      }, {
        "Column1": 1986,
        "total": 6
      }, {
        "Column1": 1987,
        "total": 4
      }, {
        "Column1": 1988,
        "total": 8
      }, {
        "Column1": 1989,
        "total": 15
      }, {
        "Column1": 1990,
        "total": 8
      }, {
        "Column1": 1991,
        "total": 4
      }, {
        "Column1": 1992,
        "total": 9
      }, {
        "Column1": 1993,
        "total": 6
      }, {
        "Column1": 1994,
        "total": 8
      }, {
        "Column1": 1995,
        "total": 4
      }, {
        "Column1": 1996,
        "total": 4
      }, {
        "Column1": 1999,
        "total": 1
      }]
    },
    "Table2": {
      "campos": ["bandera", "mensaje"],
      "datos": [{
        "bandera": 0,
        "mensaje": "Se ingresaron los datos correctamente"
      }]
    }
  },
  "tablas": null,
  "bandera": "0",
  "mensaje": "--"
}

As always, I am infinitely grateful for your help, I love you: D

    
asked by E.Rawrdríguez.Ophanim 10.04.2018 в 17:47
source

1 answer

0

If you are working with chartjs, you need the arrays to obtain the data for the label and another for the results associated with those labels. For this, from the json that you have, you can generate those array in the following way:

//Array para guardar los años.
var column1 = [];
//Array para guardar los totales.
var total = []
var datos = (Object.values(Object.values(Object.values(mjson)[1])[0])[1]);
for (var i = 0; i < datos.length; i++) {

  column1.push(Object.values(datos[i])[0]);
  total.push(Object.values(datos[i])[1]);

}
//Mostrar los array's. El primer array supongo que podes usarlo para los labels de tu gráfico, y el segundo para los datos.
console.log(column1);
console.log(total);

Then you only have to pass those arrays to graph with chartjs. It is not necessary to have a variable for each attribute.

var mjson = {
  "DatosEnvio": {},
  "DatosEnvioJson": {
    "Table1": {
      "campos": ["Column1", "total"],
      "datos": [{
        "Column1": 1956,
        "total": 1
      }, {
        "Column1": 1963,
        "total": 2
      }, {
        "Column1": 1966,
        "total": 1
      }, {
        "Column1": 1967,
        "total": 2
      }, {
        "Column1": 1968,
        "total": 1
      }, {
        "Column1": 1969,
        "total": 3
      }, {
        "Column1": 1970,
        "total": 2
      }, {
        "Column1": 1971,
        "total": 2
      }, {
        "Column1": 1972,
        "total": 3
      }, {
        "Column1": 1973,
        "total": 3
      }, {
        "Column1": 1974,
        "total": 1
      }, {
        "Column1": 1975,
        "total": 2
      }, {
        "Column1": 1976,
        "total": 1
      }, {
        "Column1": 1977,
        "total": 8
      }, {
        "Column1": 1978,
        "total": 2
      }, {
        "Column1": 1979,
        "total": 8
      }, {
        "Column1": 1980,
        "total": 6
      }, {
        "Column1": 1981,
        "total": 9
      }, {
        "Column1": 1982,
        "total": 8
      }, {
        "Column1": 1983,
        "total": 3
      }, {
        "Column1": 1984,
        "total": 3
      }, {
        "Column1": 1985,
        "total": 7
      }, {
        "Column1": 1986,
        "total": 6
      }, {
        "Column1": 1987,
        "total": 4
      }, {
        "Column1": 1988,
        "total": 8
      }, {
        "Column1": 1989,
        "total": 15
      }, {
        "Column1": 1990,
        "total": 8
      }, {
        "Column1": 1991,
        "total": 4
      }, {
        "Column1": 1992,
        "total": 9
      }, {
        "Column1": 1993,
        "total": 6
      }, {
        "Column1": 1994,
        "total": 8
      }, {
        "Column1": 1995,
        "total": 4
      }, {
        "Column1": 1996,
        "total": 4
      }, {
        "Column1": 1999,
        "total": 1
      }]
    },
    "Table2": {
      "campos": ["bandera", "mensaje"],
      "datos": [{
        "bandera": 0,
        "mensaje": "Se ingresaron los datos correctamente"
      }]
    }
  },
  "tablas": null,
  "bandera": "0",
  "mensaje": "--"
};
//Array para guardar los años.
var column1 = [];
//Array para guardar los totales.
var total = []
var datos = (Object.values(Object.values(Object.values(mjson)[1])[0])[1]);
for (var i = 0; i < datos.length; i++) {

  column1.push(Object.values(datos[i])[0]);
  total.push(Object.values(datos[i])[1]);
 
}
//Mostrar los array's. El primer array supongo que podes usarlo para los labels de tu gráfico, y el segundo para los datos.
console.log(column1);
console.log(total);
    
answered by 10.04.2018 в 19:12