繁体   English   中英

需要帮助访问JSON数组对象

[英]Need help accessing JSON array object

目标:该代码收集一个数组JSONAPIS并将该APIS传递到$ .each()循环中。 然后,在if语句中评估JSON数据字段并用于计算precip。 如何从obj访问JSONAPIS。 主要问题:每日数组成员上的obj.daily.data.length未定义。 obj应该包含要使用的API调用之一和JSON数据集。 相反,它包含诸如abort之类的关键字,总是承诺,我不熟悉该如何使用。 什么将访问结果JSON对象属性?

var listAPIs = "";
var darkForecastAPI = [];
var result = [];
var JSONAPIS = [];

$.each(numDaysAPITimes, function(a, time) {
    var darkForecastAPI = /*"http://api.wunderground.com/api/" + currentAPIKey + "/history_" + time + "/q/" + state + "/" + city +".json?callback=?"; */
        "http://api.forecast.io/forecast/" + currentAPIKey + "/" + city + time + "?callback=?";
    //https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE,TIME
    JSONAPIS.push($.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"
    }));
});
$.when.apply($, JSONAPIS).done(function(result) { /*no log simply an array */
    var eachPrecipSum = 0.0;
    var totalPrecipSinceDate = 0.0;
    alert(result);

    $.each(result, function(d, obj) {
    console.log(obj);
        for (var c = 0; c <= obj.daily.data.length - 1; c++) {
            if (obj.daily.data[c].precipIntensity >= 0.0000 && obj.daily.data[c].precipType == "rain") /*Number(result.history.dailysummary.precipm, result.history.dailysummary.rain*/ {
                eachPrecipSum = result[d].daily.data[c].precipIntensity;
                totalPrecipSinceDate = eachPrecipSum + totalPrecipSinceDate; ///Write mean precip
                alert(Math.round(eachPrecipSum * 10000) / 10000);
                $("body").append("p").text("There has been as least a total of " + Math.round(totalPrecipSinceDate * 10000) / 10000 + " inches per hour of rain at the location in the last " + userDataDatePick + " days")

            } else if (obj.daily.data[c].precipIntensity >= 0.0000 && obj.daily.data[c].precipType != "rain") {
                alert("There is was no rain on ____" /*+ result.history.dailysummary.mon + "/" + result.history.dailysummary.mday + "/" + result.history.dailysummary.year*/ );
            }
        }
    });
});
numDaysAPITimes = 0;

}

$.when不接受数组作为输入

由于您传递的数组本身并不是一个承诺,因此它可能会立即触发,因此在所有ajax调用完成之前

需要更改为

$.when.apply(null, JSONAPIS).done...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM