sterg sterg - 4 months ago 36
Javascript Question

Weather API and JSON

So,I am trying to use the Open Weather API:

Here is my 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=""+position.coords.latitude+"&lon="+position.coords.longitude+"&appid=18c7e2b6b0150a8f1d2c6b946e065697";
$.getJSON(ur, function(json) {




Here is the expected output:

"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],

The output is displayed correctly in my test page but the alert([main]); doesn't work and I want to know how can I access the particular keys of my JSON Object.For example if I want to access the id shouldn't the following do it for me:; ?

Answer is an array: = [{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}]

An array is a container object that holds a number of values of a multiple types in Javascript, to access these values you must specified Integer index.[0] = {"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}[0] is an Javascript Object and you must specify the property name, you can access the properties in two ways:

  • jsonObject["propertyName"]
  • jsonObject.propertyName


Just change this: