I'm using .getJSON to pull in data to graph. For some reason when I try to loop through the JSON array it gives me each individual character like it's not seeing it as an array, yet when I dump data to console it's properly formatted JSON.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$.getJSON("/getgraphdata", function(result) {
console.log(result.length);
testFunction(result);
});
</script>
</head>
<body>
<script>
function testFunction(data) {
console.log(data); // line 16
//console.log(data.length); Gives total character count.
for(var i = 0; i < data.length; i++) {
var obj = data[i];
//console.log(obj.id);
}
}
</script>
</body>
</html>
Here is the data directly from console. ( from line 16 )
[{"id":"1375857","temperature":"78.98","humidity":"90.2","nodeName":"Bsmt_Front","timestamp":"1536424185"},{"id":"1375856","temperature":"78.98","humidity":"77.1","nodeName":"Bsmt_Back","timestamp":"1536424185"},{"id":"1375855","temperature":"77.54","humidity":"49.9","nodeName":"Living_Room","timestamp":"1536424180"},{"id":"1375854","temperature":"0","humidity":"0","nodeName":"Bsmt_Room","timestamp":"1536424179"},{"id":"1375853","temperature":"79.52","humidity":"82.7","nodeName":"Flow_Tent","timestamp":"1536424158"}]
I used the highest rated answer here JavaScript loop through json array?
If I comment out line 21 it shows undefined. If I do a console.log(obj) it iterates through every single character in the array.
Everything points to it not seeing that as an array, but the [] are there.