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 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败