簡體   English   中英

讀取多個JSON API頁面並解析數據

[英]Read multiple JSON API Pages and parse data

目標 :要從預測API收集JSON數據,然后在指定的天數內讀取JSON precipIntensity屬性,此代碼從三開始。 由於要采取一系列一致的步驟,請嘗試使所有代碼有意義。

我的主要問題是嘗試命名返回的JSON代碼頁,然后將其置於另一個上下文中以讀取precipIntensity屬性。

概述:回溯日期獲取UNIX時間,然后為每個預報日請求一個API。 然后將API放入數組中。 將該數組放入for()循環中以請求每個JSON腳本...(現在該怎么辦?我希望能夠讀取每個JSON或計算出一些內容,但是我不知道如何請求格式化的代碼。可以做剩余的位)。 可以在我的其他相關文章中找到JSON示例... https://stackoverflow.com/questions/29949454/store-json-api-object-data-and-reuse-it (我發現API服務器存儲了自15年5月1日以來編輯的關於我的數據...已解決):

    //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 );

您的循環應如下所示:

$.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
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM