Reputation: 155
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
Reputation: 489
You have to follow the next steps for retrieving value from the JSON from API.
data
as an Object
.weather
of data
as an Array
.nth
item you need in the Array
as an Object
.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