gpt4 book ai didi

javascript - 需要帮助访问 JSON 数组对象

转载 作者:行者123 更新时间:2023-12-02 16:12:07 25 4
gpt4 key购买 nike

目标:此代码收集 JSONAPIS 数组并将 APIS 传递到 $.each() 循环中。然后在 if 语句中评估 JSON 数据字段并用于计算 precip。如何从 obj.json 访问 JSONAPIS主要问题:每日数组成员上未定义 obj.daily.data.length。该 obj 应包含 API 调用之一和要使用的 JSON 数据集。相反,它包含像 abort、always、promise 这样的关键字,我不熟悉如何使用。什么会访问结果 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 不将数组作为输入

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

需要更改为

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

关于javascript - 需要帮助访问 JSON 数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30066845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com