0

I am trying to check a JSON for the "start" object, and what it value is.

For example, if my AJAX is

$(document).ready(function () {
    $.ajax({
        url: "Content/events/document.json",
        type: "GET",
        success: function (resp) {
            alert(JSON.stringify(resp)); //Stringify'ed just to see JSON data in alert
        },
        error: function () {
            alert("failed");
        }
    });
});

and it returns

[
 {"title":"Bi-weekly Meeting1","start":"2014-07-09","color":"red"},
 {"title":"Bi-weekly Meeting2","start":"2014-08-06","color":"red"},
 {"title":"Bi-weekly Meeting3","start":"2014-07-23","color":"red"},
 {"title":"Test Event","url":"http://google.com/","start":"2014-07-28"}
]

How can I check every "start" value? and if it is today, store that event in a different array?

I just want to keep track of today's events and I am not sure how to iterate through a JSON Object.

Austin
  • 3,010
  • 23
  • 62
  • 97
  • please, take a look on [this](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript), to convert JSON to object you have to use JSON.parse() – Sergey Jul 29 '14 at 13:49
  • But won't that only grab the first item if that context exists? There could be dozens of events with the current day's date. – Austin Jul 29 '14 at 13:51
  • I would recommend to return from the server already sorted events, i.e. only for today, to avoid parsing dates on client side – Sergey Jul 29 '14 at 13:57
  • The problem is that this is a direct file grab, allowing users (non-techy) users to just modify the file for events. Since I have less-techy client who just wants to be able to edit a simple text file I unfortunately have to put a little more work on the program. So on that note, is there a simple way to just check all the `start`'s? – Austin Jul 29 '14 at 14:00
  • 1
    Once parsed into a javascript array, this is no different than iterating through any array in javascript. You have not even shown what attempt you have made to iterate this array. Can you show some code? – Mike Brant Jul 29 '14 at 14:00
  • @MikeBrant Thats what I thought, but I am newer to AJAX and know I know there can be issues with asynchronous stuff. How should I structure it so that no conflicts arise with the JS sort running before the AJAX call? Just stick it in `success`? – Austin Jul 29 '14 at 14:02
  • @Austin I am not sure what you mean by "JS sort running before the AJAX call". You would simply iterate through the array in your success handler (or a function called by your success handler), and filter the array appropriately (no need to sort that I can tell). – Mike Brant Jul 29 '14 at 14:04
  • @MikeBrant Sorry, I understand what to do, I am just still having a rough time figuring out how JSON and AJAX work together, along with how to parse it sometimes, and where at. Sorry if I confused you, I appreciate the help! Looking at mccannf's answer it looks like calling a javascript function in the ajax sucess will not have any asynchronous issues. I recently tried having a ajax call update a global js variable, and that caused issues with other js functions, silly noob mistakes. :/ – Austin Jul 29 '14 at 14:09

1 Answers1

2

Note you should set dataType: "json" so that JQuery will parse the ajax response coming back as JSON automatically. Then just iterate through the array you receive, like so:

function sameDay( d1, d2 ){
  return d1.getUTCFullYear() == d2.getUTCFullYear() &&
         d1.getUTCMonth() == d2.getUTCMonth() &&
         d1.getUTCDate() == d2.getUTCDate();
}

$(document).ready(function () {
    $.ajax({
        url: "Content/events/document.json",
        type: "GET",
        dataType: "json",
        success: function (resp) {
            resp.forEach(function(item) {
                console.log(item.start);
                if (sameDay( new Date(item.start), new Date)){
                   // This one has today's date!
                }
            });
        },
        error: function () {
            alert("failed");
        }
    });
});
mccannf
  • 16,619
  • 3
  • 51
  • 63
  • I wondering if you can dumb down (in English) what the difference is between `resp.forEach(item)` and making it a function, like `resp.forEach(function(item)`. I guess I am still confused on Javascript functions where they combine/wrap other functions. all the `});` stuff. Sorry, still newer to some of this stuff. Otherwise it makes perfect sense! :) If that just confuses you, sorry! Thank you – Austin Jul 29 '14 at 14:13
  • 1
    No problems. [forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) takes a callback function as an argument. so you in fact are providing a small Javascript function that the JS engine calls for every element in the array (in this case `resp`) and when it calls this function it passes in the current element (in this case we are calling it `item`). – mccannf Jul 29 '14 at 14:22
  • interesting! so `item` itself is each "snippet" of that JSON, rather than just the whole JSON itself? i.e. `item` is just one of the lines mentioned above in my thread JSON output? – Austin Jul 29 '14 at 14:23
  • Exactly. `resp` is the entire array of objects. forEach iterates through the array passing in each element of the array as `item` to the callback function in the order they appear in the JSON. – mccannf Jul 29 '14 at 14:25