1

I am using this JavaScript:

$.getJSON("/aTest.json", function (jsonObj) {
    $("#testJSONBtn").click(function () {
        var val = "";
        for (var i = 0; i <= jsonObj.events.length; ++i) {
            val += jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>";
        }
        $("#JSONOutput").append(val);
    });
});

To access a json file:

{
    "events":
    [
        {"title":"Okmulgee Public Schools Starts 3rd Quarter" , "date":"1-2-2013" , "explanation":"Okmulgee Public Schools begins its third quarter."},
        {"title":"Okmulgee Public Schools-Closed in Observance of Martin Luther King Jr. Holiday" , "date":"1-21-2013" , "explanation":"The Okmulgee Public Schools will be closed in observance of the Martin Luther King Jr. holiday."},
        {"title":"Okmulgee Public Schools County Professional Day" , "date":"2-1-2013" , "explanation":"Okmulgee Public Schools County Professional Day is today."}
    ]
}

After finally getting IIS Express to serve up json files, I have looked the syntax over a thousand times and can't see any errors, however when I try to get this I get:

Uncaught TypeError: Cannot read property 'title' of undefined

It errors on this line of the JavaScript/jQuery function:

val += jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>";

I'm at a loss, I have researched the following webpages and StackOverflow Questions:

Why is my JSON object undefined?

JSON returning as undefined

calling Json data returns undefined "

http://api.jquery.com/jQuery.getJSON/

http://www.w3schools.com/json/default.asp

The syntax seems right when matched up with W3Schools' example.

Community
  • 1
  • 1
VoidKing
  • 6,282
  • 9
  • 49
  • 81
  • What does `console.log(jsonObj.events[i])` return? How about `console.log(jsonObj)`? – Blender Jan 03 '13 at 22:54
  • Why are you subscribing to a button click event handler inside your AJAX success callback? – Darin Dimitrov Jan 03 '13 at 22:55
  • @Blender I'm very sorry, but I don't know how to get that information. – VoidKing Jan 03 '13 at 22:55
  • 1
    @VoidKing: Put it in your loop and open up your JS console: http://stackoverflow.com/questions/4743730/what-is-console-log-and-how-do-i-use-it – Blender Jan 03 '13 at 22:55
  • @Darin Dimitrov Because of an example I found of 'Neal's' here on StackOverflow. Found here: http://stackoverflow.com/questions/14128700/get-an-array-of-objects-from-an-external-json-file-and-store-as-an-array-in-java --- I know this probably seems like a very similiar question, and while it's the same code, it was a completely different question/problem/solution – VoidKing Jan 03 '13 at 22:56
  • @Blender, if he doesn't know what console.log means it is very likely that he doesn't know what a JS console means. – Darin Dimitrov Jan 03 '13 at 22:56
  • @Blender & Darin I have the JavaScript console open (I think??) What I mean is that I do the 'inspect element' [using chrome] and then find the script tag. and that's all of the debugging my limited knowledge really knows how to do with the browser tools (okay, I can see a little more than that, but, for the most part this is all I know how to look at in the browser). – VoidKing Jan 03 '13 at 22:58
  • 1
    @VoidKing: The JS console button is on the far right of the toolbar. When you `console.log()` something from within your script, it'll pop up in the console: http://i.imgur.com/cn9tF.png – Blender Jan 03 '13 at 23:02

1 Answers1

6

You've got an extra = in here:

for (var i = 0; i <= jsonObj.events.length; ++i) {

Should be:

for (var i = 0; i < jsonObj.events.length; ++i) {

You're overrunning your array.

Alexander
  • 23,432
  • 11
  • 63
  • 73
Hector Scout
  • 1,388
  • 2
  • 14
  • 31
  • @Both of you. You don't know how long I've been stuck on this! There was another problem getting the server to serve up json files, but your answer here is correct! It says I can't accept your answer for like 5 more minutes, but I have to clock out and leave work now. I PROMISE I WILL ACCEPT THIS first thing tomorrow morning! (I did test and it did work!) THANK YOU THANK YOU THANK YOU! (Sorry for the lame oversight on overrunning my array). – VoidKing Jan 03 '13 at 23:01