[英]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.