Danny
Danny

Reputation: 155

openweather API .getJSON request returning undefined

I've read the a lot of similar topics but they haven't helped..

Trying to access this JSON object..

{

    "coord": {
        "lon": -83.9,
        "lat": 33.99
    },
    "weather": [
        {
            "id": 701,
            "main": "Mist",
            "description": "mist",
            "icon": "50n"......

With this script. I have linked to the jQuery CDN, made sure my api key is right, just at a loss here.

<html>

 <script
              src="https://code.jquery.com/jquery-2.2.4.min.js"
              integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
              crossorigin="anonymous"></script>

<script>$(document).ready(function(){
  var api = "http://api.openweathermap.org/data/2.5/weather?lat=33.9654688&lon=-83.9138728&appid=(This is where the apikey goes)";

  $.getJSON(api, function(data){
    console.log(api.weather.main);
  });
});
</script>
</html>

Any help is much appreciated, this is my first time doing api calls so it's a big ol roadblock for me right now.

Upvotes: 2

Views: 590

Answers (2)

Anson
Anson

Reputation: 489

You have to follow the next steps for retrieving value from the JSON from API.

  1. Specify data as an Object.
  2. Retrieve weather of data as an Array.
  3. Retrieve nth item you need in the Array as an Object.
  4. Retrieve main of nth Object as an String.

See the example of above description.

var mainObj = {
    "coord": {
        "lon": -83.9,
        "lat": 33.99
    },
    "weather": [
        {
            "id": 701,
            "main": "Mist",
            "description": "mist",
            "icon": "50n"
        }
    ]
}

console.log(mainObj);
console.log(mainObj.weather);
console.log(mainObj.weather[0]);
console.log(mainObj.weather[0].main);

Upvotes: 1

Jaime Asm
Jaime Asm

Reputation: 141

Maybe you should read data like console.log(data);?

Upvotes: 2

Related Questions