Trying to integrate with the Open Weather API:
Check out this snippet of javascript code:
$(document).ready(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$(".ok").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
var ur="http://api.openweathermap.org/data/2.5/weather?lat="+position.coords.latitude+"&lon="+position.coords.longitude+"&appid=18c7e2b6b0150a8f1d2c6b946e065697";
$.getJSON(ur, function(json) {
$(".ok2").html(JSON.stringify(json));
alert(json.weather[main]);
});
});
}
});
This is what the expected output should look like:
{"coord":{"lon":139,"lat":35},
"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
"wind":{"speed":7.31,"deg":187.002},
"rain":{"3h":0},
"clouds":{"all":92},
"dt":1369824698,
"id":1851632,
"name":"Shuzenji",
"cod":200}
Everything seems to display correctly on my test page but the issue arises with the alert(json.weather[main]); It's not working as expected. How can I properly access specific keys in my JSON Object? For instance, would writing json.id give me the desired result?