Accessing Deeper JSON Objects

Accessing Deeper JSON Objects
0

#1

Please could somebody help me with accessing nested JSON objects with the OpenWeather API? Anything in the city object I can access successfully, but I can’t seem to access anything else below it.

This is my code (currently using a click event while I work out the basics of this project):

$.getJSON("http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=mykey" , function(data) {

  // Replace Placeholders With JSON Objects
  $("#city").hide().html(data.city.name + ", ").fadeIn('slow');

  $("#country").hide().html(data.city.country).fadeIn('slow');

  $("#temperature").hide().html(data.main.list.temp).fadeIn('slow');

});

This is the JSON, I want to access the temperature much like I successfully accessed the city and country, but I can’t work out what to use.

{
"city":{
"id":524901,
"name":"Moscow",
"coord":{
"lon":37.615555,
"lat":55.75222
},
"country":"RU",
"population":0,
"sys":{
"population":0
}
},
"cod":"200",
"message":0.0089,
"cnt":38,
"list":[
{
"dt":1475388000,
"main":{
"temp":290.64,
"temp_min":283.046,
"temp_max":290.64,
"pressure":1014.5,
"sea_level":1034.35,
"grnd_level":1014.5,
"humidity":86,
"temp_kf":7.59

#2

Try this:

data.list[0].main.temp

EDIT:

You can use something like JSON Lint to view a nicer JSON or use JSONView Chrome plugin.


#3

Thank you! The JSON viewer I was using wasn’t showing the arrays very well. Working now.