jonnyhitek
jonnyhitek

Reputation: 1551

Problem parsing JSON

Hi got a weird problem parsing JSON for some reason can't access the values contained, can't see that I'm doing anything wrong but guessing i'm doing something silly any help would be greatly appreciated.

Json -

{
    "toilClaims": [
        {
            "id": "1",
            "name": "jonathan",
            "date": "12/12/2011",
            "hours": "8",
            "reason": "asdasda"
        },
        {
            "id": "2",
            "name": "jonathan",
            "date": "12/12/2011",
            "hours": "3",
            "reason": "sdasdasdasd"
        },
        {
            "id": "3",
            "name": "trvor",
            "date": "12/2/2",
            "hours": "we",
            "reason": "asda"
        }
    ]
}

I won't include all js but in a return function from a ajax call (data comes back fine), I simply try the following:

function listSetup(data, refresh) {
        console.log(data.toilClaims.id[2]);

}

How ever it fails to find the id, I can however access data.toilClaims ???

Help lol

Upvotes: 0

Views: 185

Answers (2)

Kumar
Kumar

Reputation: 5147

As pointed by @dante617, toilClaim is an array, just by looking at square brackets you can tell that its an array or objects.

Upvotes: 0

MikeTheReader
MikeTheReader

Reputation: 4190

In this case, toilClaims itself is the array. Try this:

console.log(data.toilClaims[2].id);

Upvotes: 1

Related Questions