Marlin Hankin
Marlin Hankin

Reputation: 61

How do you read in this json array?

{
    "1370" : ["Tomai", "Grabowski", "Chebotko", "Egle"],
    "2380" : ["Schweller", "Chen", "Tomai"],
    "3333" : ["Schweller", "Chen", "The Devil"]
}

I would assume that you would access say chebotko by 1370[2] but it isn't giving me anything. What am I doing wrong?

This is how I am accessing it.

$.getJSON("instructors.json", function(data) {
    console.log(data);
    // data is a JavaScript object now. Handle it as such

});

Upvotes: 1

Views: 81

Answers (1)

David Hedlund
David Hedlund

Reputation: 129792

1370 is a property of the object. The object itself needs to be referenced in some kind of variable. var myObject = { '1370': ... }, or if it's a response from an AJAX request, you'll access that as an input parameter to your callback function. Either way, you need to first reference the object itself, then its properties:

alert(myObject['1370'][2]) // 'Chebotko'

Upvotes: 4

Related Questions