0

所以我想得到以下数据:

仅适用于一个城市,例如伦敦 今天温度 明天最低和最高温度 明天之后最低和最高温度

然后我抓取这些值并将其附加到 html 中。问题是,如何过滤从 json 请求接收到的数据?这是我的示例代码,我已经有了今天温度的数据,但是如何过滤接下来几天的最低和最高温度呢?

太感谢了

jQuery( document ).ready( function() {


function getWeather(callback) {
    var weather = 'http://openweathermap.org/data/2.5/forecast?lat=35&lon=139&units=metric';
    jQuery.ajax({
      dataType: "jsonp",
      url: weather,
      success: callback
    });
}

// get data:
getWeather(function (data) {
    console.log(data);
    console.log(data.list[0].main.temp);
    console.log(data.list[0].main.temp_min);
    console.log(data.list[0].main.temp_max);
});

// append data to html
jQuery('span.customWeather').text('weather here!');


});
4

2 回答 2

1

我遇到了一个类似的障碍,因为我在 openweathermap.org/forecast 的示例中使用的 url 中包含了“daily”

http://api.openweathermap.org/data/2.5/forecast/daily?q=London&units=metric&cnt=7

这种风格的 url 返回一个非常简洁的 json 对象,没有“dt_txt”给出预测日期,只是令人困惑的(对新手来说)unix 时间数字,它不是预测的适用日期,而是检索数据时的时间戳从气象站。

通过省略“每日”,即

http://api.openweathermap.org/data/2.5/forecast?q=London&units=metric&cnt=7

带来了更详细的 json 响应,其中包括 dt_txt。

于 2014-09-16T21:54:36.090 回答
0

好吧,我没有数据集 - 但是 - 获取 JSON 数据的 url 在浏览器中打开它,只需将其发布在窗口中即可。

我这样做了,这是我得到的一个片段(没有阅读该网站的文档)

{"cod":"200","message":0.3729,"city":{"id":1851632,"name":"Shuzenji","coord":{"lon":138.933334,"lat":34.966671 },"country":"JP","population":0},"cnt":41,"list":[{"dt":1394992800,"main":{"temp":-6.78,"temp_min" :-6.78,"temp_max":-6.78,"压力":935.65,"sea_level":1036.67,"grnd_level":935.65,"湿度":73,"temp_kf":0},"天气":[{"id ":800,"main":"Clear","description":"天空晴朗","icon":"01n"}],"clouds":{"all":0},"wind":{"速度":0.71,"度":128.005},"系统":{"pod":"n"},"dt_txt":"2014-03-16 18:00:00"},{"dt":1395003600,"main":{"temp":-8.05,"temp_min":- 8.05,"temp_max":-8.05,"压力":936.88,"sea_level":1038.77,"grnd_level":936.88,"湿度":72,"temp_kf":0},"天气":[{"id": 800,"main":"Clear","description":"天空晴朗","icon":"01n"}],"clouds":{"all":0},"wind":{"speed" :0.61,"deg":175.004},"sys":{"pod":"n"},"dt_txt":"2014-03-16 21:00:00"},{"dt":1395014400,"主要的”:{"temp":-8.05,"temp_min":-8.05,"temp_max":-8.05,"pressure":936.88,"sea_level":1038.77,"grnd_level":936.88,"湿度":72,"temp_kf": 0},"weather":[{"id":800,"main":"Clear","description":"天空晴朗","icon":"01n"}],"clouds":{"all ":0},"wind":{"speed":0.61,"deg":175.004},"sys":{"pod":"n"},"dt_txt":"2014-03-16 21:00 :00"},{"dt":1395014400,"main":{"temp":-8.05,"temp_min":-8.05,"temp_max":-8.05,"pressure":936.88,"sea_level":1038.77,"grnd_level":936.88,"湿度":72,"temp_kf": 0},"weather":[{"id":800,"main":"Clear","description":"天空晴朗","icon":"01n"}],"clouds":{"all ":0},"wind":{"speed":0.61,"deg":175.004},"sys":{"pod":"n"},"dt_txt":"2014-03-16 21:00 :00"},{"dt":1395014400,"main":[{"id":800,"main":"Clear","description":"天空晴朗","icon":"01n"}],"clouds":{"all":0},"wind ":{"speed":0.61,"deg":175.004},"sys":{"pod":"n"},"dt_txt":"2014-03-16 21:00:00"},{" dt":1395014400,"主要":[{"id":800,"main":"Clear","description":"天空晴朗","icon":"01n"}],"clouds":{"all":0},"wind ":{"speed":0.61,"deg":175.004},"sys":{"pod":"n"},"dt_txt":"2014-03-16 21:00:00"},{" dt":1395014400,"主要":

我搜索并搜寻并使用名为人眼的 Mark 1 搜索引擎找到了日期参考。

在那里我找到了变量 dt_txt 'dt_txt":"2014-03-16 18:00:00"。

然后,您可以访问该数组中的数据。日期 javascript 函数和数组访问是在线记录的,我不会重复它们,这是搜索您可以找到的示例。

我花了不到 3 分钟的时间,发现每一行数据都标有日期时间标签。

于 2014-03-16T19:18:33.747 回答