I am having problems accessing what for me is a complex JSON object.
I successfully interrogated the Open Weather API and received a good response.
What I am not accessing, and don’t know how to, is the individual items of the weather portion of the list group while in a for loop.
When printed the values are undefined.
Thanks for any help!
The Response from the open weather API

{
   "timestamp":1369498755320,
   "data":{
      "cod":"200",
      "message":0.0372,
      "city":{
         "id":5037649,
         "name":"Minneapolis",
         "coord":{
            "lon":-93.26384,
            "lat":44.979969
         },
         "country":"US",
         "population":382578
      },
      "cnt":10,
      "list":[
         {
            "dt":1369504800,
            "temp":{
               "day":291.59,
               "min":285.95,
               "max":291.59,
               "night":285.95,
               "eve":289.25,
               "morn":288.86
            },
            "pressure":1004.12,
            "humidity":62,
            "weather":[
               {
                  "id":800,
                  "main":"Clear",
                  "description":"sky is clear",
                  "icon":"01d"
               }
            ],
            "speed":7.27,
            "deg":154,
            "clouds":32
         },
         {
            "dt":1369591200,
            "temp":{
               "day":286.26,
               "min":285.08,
               "max":287.6,
               "night":285.8,
               "eve":286.83,
               "morn":285.08
            },
            "pressure":1001.63,
            "humidity":71,
            "weather":[
               {
                  "id":501,
                  "main":"Rain",
                  "description":"moderate rain",
                  "icon":"10d"
               }
            ],
            "speed":8.11,
            "deg":131,
            "clouds":92,
            "rain":3.5
         },
         ....
      ]
   }
}

In trying to print to the browser I use the following to access the weather section but this does not wok and gives me a value of undefined.


//Turn JSON string into a javaScript object
 weatherOutPut = JSON.parse(weather);

 $('#baker').append('weather.id = ' + weatherOutPut.data.list[i].weather.id + '<br />');
 $('#baker').append('weather.main = ' + weatherOutPut.data.list[i].weather.main + '<br />');
 $('#baker').append('weather.description = ' + weatherOutPut.data.list[i].weather.description + '<br />');
 $('#baker').append('weather.icon = ' + weatherOutPut.data.list[i].weather.icon + '<br />'); 

 //This returns undefined.

I found the syntax I was looking for.
an example is:
weatherOutPut.data.list[i].weather[0].id
Problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.