I am trying to obtain the data from a json but not when searching for a data it throws me the error.
Uncaught TypeError: Cannot 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 way jsonFinal[0]['dia']
, but I have not been able to access them.
the json to which I am querying 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'}
The format you put is wrong
For it to be an array of objects it has to be enclosed in square brackets.
Objects in JSON are enclosed in curly braces { } and within the curly braces are the properties of the objects with their respective values (in quotes) separated by : (colon)
Multiple properties are separated by commas
To be able to group several objects they have to be put between brackets (like an array) and separate each object with a comma
A good short tutorial here
Use
jsonFinal[0]['dia']
when you have an array of objects.From the information you've put in the comments, you have a single object, not an array, so the way to access the property should be:
either
If you have an array like this:
You can access the element you wanted this way
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 whatever you have