Tia
Tia

Reputation: 3

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;
    console.log("ledState="+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",
        "bedOverhead": "0",
        "bedCandle": "0",
        "bedTemp": "76.47",
        "ledState": "1",
        "fadeSpeed": "5",
        "ledBrightness": "100",
        "autoSwamp": "1",
        "goalTemp": "77",
        "colorHEX": "00ffff",
        "outsideTemp": "113",
        "watts": "39.23",
        "lvTemp": "78",
        "lvHumidity": "20",
        "lvLamp": "0",
        "lvCandle": "0",
        "lvLedState": "0",
        "lvFadeSpeed": "10",
        "lvLedBrightness": "100",
        "lvColorHEX": "ff0033"
    }
]

What am I forgetting?

Upvotes: 0

Views: 34

Answers (1)

bugwheels94
bugwheels94

Reputation: 31920

This is an Array of Objects so changing to

 var ledState = data[0].ledState;

should work

Upvotes: 1

Related Questions