I am trying to get JSON from API and then access "main" object of the "weather" object of the JSON.
When I use this code:
$.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139", function(json) {
var data = JSON.stringify(json);
alert(data);
});
I get this output:
{
"coord": {
"lon": 159,
"lat": 35
},
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "https://cdn.glitch.com/6e8889e5-7a72-48f0-a061-863548450de5%2F10n.png?1499366021399"
}],
"base": "stations",
"main": {
"temp": 22.59,
"pressure": 1027.45,
"humidity": 100,
"temp_min": 22.59,
"temp_max": 22.59,
"sea_level": 1027.47,
"grnd_level": 1027.45
},
"wind": {
"speed": 8.12,
"deg": 246.503
},
"rain": {
"3h": 0.45
},
"clouds": {
"all": 92
},
"dt": 1499521932,
"sys": {
"message": 0.0034,
"sunrise": 1499451436,
"sunset": 1499503246
},
"id": 0,
"name": "",
"cod": 200
}
Now, the output that I am trying to get is "Rain" (the property of the "main" object of the "weather" object (I hope I said this correctly, I'm a beginner)).
So logically, I would do this:
$.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139", function(json) {
var data = JSON.stringify(json);
alert(data["weather"].main);
});
But that doesn't give me any output.
I did some search, and found out that I should parse.
But when I did:
$.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139", function(json) {
var data = JSON.stringify(json);
var Jason = JSON.parse(data);
alert(Jason["weather"].main);
});
I got undefined as my output again.
So, what should my code look like so my output would be "Rain"?
PS: Sorry if I made mistakes in describing my issue, I am really new to JavaScript/jQuery and also English is my second language.