Go through data from a json with javascript

2

I'm trying to get the data of a json but not when looking for a data throws me the error.

  

Uncaught TypeError: Can not read property 'day' of undefined

$.ajax({
  url: url,
  //dataType: 'json',
  type: 'POST',
  jsonpCallback: "myJSON",
  success: function (json) {
    alert(json);
    var subjson = json.substring(7, json.length-1);
    alert(subjson);
    var jsonValido = JSON.stringify(eval("(" + subjson + ")"));
    var jsonFinal = JSON.parse(JSONize(jsonValido));
    alert(jsonFinal);
    //$("#resultado").html(json);
    alert("obtencion de datos: Dia" + jsonFinal[0]['dia'] + " A: " + jsonFinal[0]['A']);
    $("#resultado").html(jsonFinal[0]);
  }

I have reviewed how to obtain data and I saw that this is the jsonFinal[0]['dia'] form, but I have not managed to access them.

The json to which I am consulting the data has this format

{'dia':'01-08-2016','A':'3','B':'5','C':'33','D':'34','E':'3'},{'dia':'02-08-2016','A':'3','B':'3','C':'38','D':'30','E':'3'}
    
asked by Stevn 23.08.2017 в 17:32
source

3 answers

3

The format you put is incorrect

{'dia':'01-08-2016','A':'3','B':'5','C':'33','D':'34','E':'3'},{'dia':'02-08-2016','A':'3','B':'3','C':'38','D':'30','E':'3'}

To be an arrangement of objects it has to be enclosed in square brackets

[{'dia':'01-08-2016','A':'3','B':'5','C':'33','D':'34','E':'3'},{'dia':'02-08-2016','A':'3','B':'3','C':'38','D':'30','E':'3'}]

Objects in JSON are enclosed in {} keys and inside the keys are the properties of the objects with their respective values (in quotes) separated by : ( two points)

{ propiedad:"valor" }

Several properties are separated by commas

{ propiedad:"valor", otro:"su valor" }

To be able to group several objects they have to be put in brackets (as an array) and separate each object with a comma

[ {propiedad:"valor"} , {otra:"valor"} ]

A good short tutorial here

    
answered by 23.08.2017 / 19:53
source
2

Use jsonFinal[0]['dia'] when you have an array of objects.

Because of the information you have put in the comments, you have only one object, not an arrangement, so the way to access the property should be:

jsonFinal['dia'] 

or

jsonFinal.dia
    
answered by 23.08.2017 в 18:00
1

If you have an arrangement like this:

[{'dia':'01-08-2016','A':'3','B':'5','C':'33','D':'34','E':'3'},{'dia':'02-08-2016','A':'3','B':'3','C':'38','D':'30','E':'3'}]

You can access the item you wanted in this way

//jsonFinal es el objeto devuelto por la solicitud del ajax que anteriormente mencionabas
var dia = jsonFinal[0].dia;

If you want to access any other property you just have to call it after the point      var A = jsonFinal [0] .A; // or .B or anyone who has

    
answered by 23.08.2017 в 18:01