繁体   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