1
{
    "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

});

1 Answer 1

4

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'
Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to get it from a file that I put in the same directory called instructor.json and that text is all that is inside it. I can change it if need be but I notice my console recongizes it but only as Ojbect. If I add var professor = to it, it won't work.
@MarlinHankin: As you're saying in your updated question, data is the reference to the object. That's the variable name you want to use. alert(data['1370'][2]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.