【问题标题】:Need help accessing JSON array object需要帮助访问 JSON 数组对象
【发布时间】:2015-07-16 00:08:37
【问题描述】:

目标:此代码收集一个数组 JSONAPIS 并将 APIS 传递到 $.each() 循环。然后 JSON 数据字段在 if 语句中进行评估并用于计算 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;

}

【问题讨论】:

    标签: javascript jquery arrays json each


    【解决方案1】:

    $.when 不将数组作为输入

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

    需要改成

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

    【讨论】:

    • 把这个放在同一个上下文中?
    • 喜欢这个? $.when.apply(null, JSONAPIS).done(function(result) {
    • 这从列表中产生了一个对象,但它构建了第二个项目,称为成功,另一个类似于我使用关键字的第一个代码。您认为发生了什么或需要做什么?它应该生成三个 JSON 对象
    • 看看这个里面做的console.log(arguments)应该是所有ajax响应的数组
    • 这就是发生的事情。 0: Object 1: "success" 2: Object您的意思是在代码中的某处添加console.log(arguments)?它是否显示弹出窗口?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多