0

So,I am trying to use the Open Weather API: http://openweathermap.org/current

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="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]);

        });

  });
}
});

Here is the expected output:

{"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}


The output is displayed correctly in my test page but the alert(json.weather[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: json.id; ?

sterg
  • 15
  • 3

2 Answers2

2

json.weather is an array:

json.weather = [{"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.

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

json.weather[0] is an Javascript Object and you must specify the property name, you can access the properties in two ways:

  • jsonObject["propertyName"]
  • jsonObject.propertyName

So

Just change this:

alert(json.weather[main]);

With:

alert(json.weather[0].main);
David Antoon
  • 815
  • 6
  • 18
0

You can access a property of an object in JavaScript in two ways. First, using dot-notation:

object.property // after the dot, literally write the name of the property

and second, using brackets:

object["property"] // in the [], put any expression

Brackets take any expression, and use the value of that expression as the name of the property to access. So writing weather[main] first evaluates the expression in the brackets: main. This is a variable name, so it will evaluate to the value of the main variable (or, if main doesn't exist, will throw an error).

If you want to access a property with a fixed name, you should generally use dot-notation. So alert(json.weather[main]); should be alert(json.weather.main);. Simple as that.

Brackets are used when the name of the property to be accessed either (a) isn't a valid identifier, e.g. contains special characters, or (b) isn't fixed, e.g. depends on variables or such.

qxz
  • 3,814
  • 1
  • 14
  • 29