0

Objective: To collect JSON data from forecast API and then read the JSON precipIntensity property over the number of days specified, this code starts at three. Since this take a number of steps to coherently follow please try to make sense of all the code.

My main issue is trying to name the JSON code pages that return then put them into another context to read the precipIntensity property.

To outline: The back date gets the UNIX time, then requests an API for each forecast day. Then the APIs are put in an array. The array is put in a for() loop to request each JSON script... (now what to do? I would like to be able to read each or calculate something but I do not know how to ask for the formatted code. I can do the remaining bit). A sample of JSON can be found at my other related post... https://stackoverflow.com/questions/29949454/store-json-api-object-data-and-reuse-it (I found that the API server stores the data for me...solved) EDITED since 5/1/15:

    //Get the back dated times and current in UNIX, 
   //later make a lookup that gives datediff from current date and user's date and adjust index i condition to equal exact days.
            var totalPrecipSinceDate;
            var threeDayAPITimes = [];

            for (var i = 0; i <= 2; i++) //place user userData-1 where i <= input
        {
            var myDate = new Date(); //https://stackoverflow.com/questions/7693170/javascript-convert-from-epoch-string-to-date-object
            var epoch = myDate.getTime(); //1318023197289 number of ms since epoch
            var unixEpoch = Math.round(epoch/1000)
            threeDayAPITimes[i] = Math.round(unixEpoch - (86400 * i));
                /*
                var epoch = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
                threeDayAPITimes[i] = Math.round(epoch - (86400 * i));
                */
        }
        //Plan to convert UNIX dates to display

        //List of locations: LATITUDE,LONGITUDE
        var locations = ["46.3494,-85.5083"]

        var currentAPIKey ="privateAPIKey"; //gets an APIkey from user from forecaster input.

 var listAPIs = "";

$.each(threeDayAPITimes, function(i, time) {
    var darkForecastAPI= "https://api.forecast.io/forecast/" + currentAPIKey + "/" + locations + "," + time;
    $.getJSON(darkForecastAPI, {
        tags: "WxAPI[" + i + "]",  //Is this tag the name of each JSON page? I tried to index it incase this is how to refer to the JSON formatted code from the APIs.
        tagmode: "any",
        format: "json"
    }, function(result) {
        // Process the result object
    });
    });
  //Process result in foreach loop   
         var eachPrecipSum = 0;
    if(result.currently.precipIntensity >=0 && result.currently.precipType == "rain")
        {
            $.each(result, function() {
              eachPrecipSum += (this.currently.precipIntensity);
              totalPrecipSinceDate += eachPrecipSum ;
        });

        }       
            alert(eachPrecipSum );
Community
  • 1
  • 1
safron6
  • 149
  • 1
  • 2
  • 13
  • what does `ask for the formatted code` mean? – charlietfl May 01 '15 at 02:36
  • `listAPIs` is a string. Why are you using `listAPIs.length` as the limit of your `for` loop? That's the total number of characters in all the concatenated URLs. Also, it should be `length`, not `Length`. – Barmar May 01 '15 at 02:36
  • The syntax of your IIFE is wrong. You have an extra `});` before `})();`. – Barmar May 01 '15 at 02:39
  • I am thinking of it in a way as an array that stores JSON, so if this API is returned as JSON then it would be named some array so I can then get for example something like WxAPI[0].precipIntensity from the JSON backdated page name then add these values in these fields up in another variable. In all these are pages of JSON, simply need to organize them with labels that the compiler can recognize. – safron6 May 01 '15 at 02:40
  • I might change part of this to a foreach loop. I am thinking the concatenation given a longitude, latitude that will have at least three API page requests generate on each location and from the same APIKey. – safron6 May 01 '15 at 02:42
  • Each time through the first `for` loop you're re-assigning `darkForecastAPI`. When the loop is done, the variable contains the last value you assigned, and the second loop will keep sending to that URL. – Barmar May 01 '15 at 02:43
  • You're concatenating the entire `darkForecastAPI` URL to `listAPIs`. – Barmar May 01 '15 at 02:44
  • It seems like you should just combine those two `for` loops. First you set `darkForecastAPI`, then you call `$.getJSON` on it. Also, if you want to do anything with the JSON, you need a callback function that processes it. – Barmar May 01 '15 at 02:45
  • Right, the listAPIs is an array object or list that saves the unique UNIX time instance from the for() loop – safron6 May 01 '15 at 02:46
  • Got an idea where to look for tutorial on callback functions? – safron6 May 01 '15 at 02:47
  • It's just the next argument to `$.getJSON`. It receives the parsed JSON response as an argument. It's explained in the jQuery documentation. Any tutorial on using AJAX with jQuery should explain it. – Barmar May 01 '15 at 02:48
  • `listAPIs` is not an array object or list. It's a string: `var listObj = ""`. – Barmar May 01 '15 at 02:48
  • So in a way call each $.getJSON at once in the same loop. – safron6 May 01 '15 at 02:49
  • What is `double[] threeDayAPITimes = new double[3];` supposed to mean? That's not Javascript, it looks like it might be Java. – Barmar May 01 '15 at 02:49
  • so abandon the listAPIs for() loop so it all happens at once and put darkForecastAPI in the $.getJSON request – safron6 May 01 '15 at 02:52
  • That is C#, that portion works correctly in Visual studio. I checked the operability of the code. It returns three UNIX Times to put in the API request URL. – safron6 May 01 '15 at 02:53

1 Answers1

0

Your loop should be something like this:

$.each(threeDayAPITimes, function(i, time) {
    var darkForecastAPI= "https://api.forecast.io/forecast/" + currentAPIKey + "/" + locations + "," + time;
    $.getJSON(darkForecastAPI, {
        tags: "WxAPI[" + i + "]",  //Is this tag the name of each JSON page? I tried to index it incase this is how to refer to the JSON formatted code from the APIs.
        tagmode: "any",
        format: "json"
    }, function(result) {
        // Process the result object
    });
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • would the result object then have to be processed in a similar scope? Since each result is a page of JSON is it possible to form an index or just process it all at once and find the precipIntensity from the result? – safron6 May 01 '15 at 03:00
  • I figured out a solution after your criticism. Hmmm. http://stackoverflow.com/questions/30091533/trying-to-form-array-of-callback-results-from-multiple-sources-of-getjson – safron6 May 08 '15 at 03:40