Question

So I would like to get the following data:

Only for one city, for example London Today temperature Tomorrow min and max temp After tomorrow min and max temp

Then I grab those values and append it to the html. The question is, how do I filter the data received from the json request? Here is my sample code, I already have the data for today temp, but how do I filter for the next days min and max temp?

Thank you so much

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!');


});
Was it helpful?

Solution 2

Well, I don't have the data set - but - take the url to the JSON data open it in a browser just post it in the window.

I did this and here is a snipet of what I got (without reading the document for the site)

{"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,"pressure":935.65,"sea_level":1036.67,"grnd_level":935.65,"humidity":73,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":0.71,"deg":128.005},"sys":{"pod":"n"},"dt_txt":"2014-03-16 18:00:00"},{"dt":1395003600,"main":{"temp":-8.05,"temp_min":-8.05,"temp_max":-8.05,"pressure":936.88,"sea_level":1038.77,"grnd_level":936.88,"humidity":72,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"sky is clear","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":

I searched and hunted and found a date reference with the Mark 1 search engine called the human Eye.

there I found the variable dt_txt 'dt_txt":"2014-03-16 18:00:00".

you can then access the data in that array. The date javascript functions and array access is documented online and I won't repeat them, this is searching for examples you can find.

It took me less than 3 minutes to find that each row of data is marked with a date time tag.

OTHER TIPS

I hit a similar snag in that I was including "daily" in the url as used in the example at openweathermap.org/forecast

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

That style of url returns a very terse json object with no "dt_txt" giving the forecast date, just the confusing (to newbies) unix time number which isn't the applicable day for the forecast, but the timestamp for when the data was retrieved from the weather station.

By leaving out the "daily", ie

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

brings a far more verbose json response which includes dt_txt.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top