Tia Tia - 1 year ago 107
jQuery Question

Failing to retrieve values from $.getJSON

I am trying to use $.getJSON to pull data from a server, but the results keep showing as undefined and I'm not sure what I'm doing wrong.

var url="http://api.<domain>.com/<access token>/";
$.getJSON(url, function(data) {

// values
var ledState = data.ledState;

This results in: "ledState=undefined" in the console. If I stringify "data", I get:

[{"id":"1098","readingTime":"2016-06-16 18:06:11","bedLamp1":"0","bedLamp2":"0",

What am I forgetting?

Answer Source

This is an Array of Objects so changing to

 var ledState = data[0].ledState;

should work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download