Jump to content


Photo

Reading From Converted Json To Javascript Object


  • Please log in to reply
1 reply to this topic

#1 williamrouse

williamrouse

    Advanced Member

  • Member
  • PipPipPipPip
  • 152 posts

Posted 25 May 2013 - 01:40 PM

I am having problems accessing what for me is a complex JSON object.<br style="margin-top: 0px; color: rgb(67, 67, 67); font-family: helvetica, arial, verdana, 'ms sans serif', sans-serif; font-size: 14px; line-height: 22px;">I successfully interrogated the Open Weather API and received a good response.<br style="color: rgb(67, 67, 67); font-family: helvetica, arial, verdana, 'ms sans serif', sans-serif; font-size: 14px; line-height: 22px;">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.<br style="color: rgb(67, 67, 67); font-family: helvetica, arial, verdana, 'ms sans serif', sans-serif; font-size: 14px; line-height: 22px;">When printed the values are undefined.<br style="color: rgb(67, 67, 67); font-family: helvetica, arial, verdana, 'ms sans serif', sans-serif; font-size: 14px; line-height: 22px;">Thanks for any help!

The

[/font][/color]
 //Turn JSON string into a javaScript
object                        weatherOutPut = JSON.parse(weather);
[color=#434343][font=helvetica, arial, verdana,]



[/font][/color]
{        "timestamp": 1369448773482,        "data": {            "cod": "200",            "message": 0.0288,            "city": {                "id": 5037649,                "name": "Minneapolis",                "coord": {                    "lon": -93.26384,                    "lat": 44.979969                },                "country": "US",                "population": 382578            },            "cnt": 10,            "list": [                {                    "dt": 1369418400,                    "temp": {                        "day": 287.94,                        "min": 283.89,                        "max": 287.94,                        "night": 283.89,                        "eve": 287.94,                        "morn": 287.94                    },                    "pressure": 1004.17,                    "humidity": 50,                    "weather": [                        {                            "id": 804,                            "main": "Clouds",                            "description": "overcast clouds",                            "icon": "04n"                        }                    ],                    "speed": 4.31,                    "deg": 168,                    "clouds": 88                },                {                    "dt": 1369504800,                    "temp": {                        "day": 283.34,                        "min": 281.45,                        "max": 285.03,                        "night": 284,                        "eve": 285.03,                        "morn": 281.45                    },                    "pressure": 1005.12,                    "humidity": 83,                    "weather": [                        {                            "id": 500,                            "main": "Rain",                            "description": "light rain",                            "icon": "10d"                        }                    ],                    "speed": 6.81,                    "deg": 150,                    "clouds": 92,                    "rain": 2                },                {                    "dt": 1369591200,                    "temp": {                        "day": 282.23,                        "min": 281.6,                        "max": 285.02,                        "night": 285.02,                        "eve": 283.93,                        "morn": 283.42                    },                    "pressure": 1001.01,                    "humidity": 100,                    "weather": [                        {                            "id": 502,                            "main": "Rain",                            "description": "heavy intensity rain",                            "icon": "10d"                        }                    ],                    "speed": 7.26,                    "deg": 121,                    "clouds": 100,                    "rain": 19                },                {                    "dt": 1369677600,                    "temp": {                        "day": 290.4,                        "min": 286.66,                        "max": 292.08,                        "night": 287.85,                        "eve": 291.5,                        "morn": 286.66                    },                    "pressure": 991.78,                    "humidity": 99,                    "weather": [                        {                            "id": 501,                            "main": "Rain",                            "description": "moderate rain",                            "icon": "10d"                        }                    ],                    "speed": 5.32,                    "deg": 129,                    "clouds": 92,                    "rain": 4                },                {                    "dt": 1369764000,                    "temp": {                        "day": 293.63,                        "min": 287.74,                        "max": 294.66,                        "night": 287.74,                        "eve": 293.81,                        "morn": 289.49                    },                    "pressure": 977.27,                    "humidity": 98,                    "weather": [                        {                            "id": 502,                            "main": "Rain",                            "description": "heavy intensity rain",                            "icon": "10d"                        }                    ],                    "speed": 7.15,                    "deg": 190,                    "clouds": 76,                    "rain": 13                },
[color=#434343][font=helvetica, arial, verdana,]



Here is the code that does not work and prints undefined for the weather valuses:
[/font][/color]
 $('#baker').append('weather.id = ' + weatherOutPut.data.list[i].weather.id + '
');                            $('#baker').append('weather.main = ' + weatherOutPut.data.list[i].weather.main + '
');                            $('#baker').append('weather.description = ' + weatherOutPut.data.list[i].weather.description + '
');                            $('#baker').append('weather.icon = ' + weatherOutPut.data.list[i].weather.icon + '
'); 
[color=#434343][font=helvetica, arial, verdana,]

  • 0

#2 williamrouse

williamrouse

    Advanced Member

  • Member
  • PipPipPipPip
  • 152 posts

Posted 25 May 2013 - 02:27 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

★★★★★ 5 Star Rated Web Developer Course - check it out now!