dongpu8935 2015-05-01 02:20
浏览 83
已采纳

读取多个JSON API页面并解析数据

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 );
  • 写回答

1条回答 默认 最新

  • duanke0178 2015-05-01 02:53
    关注

    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
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形