I have the following json data:
var JSONObject2= {
"weather": {
"curren_weather": [
{
"humidity": "69",
"pressure": "1012",
"temp": "70",
"temp_unit": "f",
"weather_code": "1",
"weather_text": "Mostly cloudy",
"wind": [
{
"dir": "Not Available",
"speed": "0",
"wind_unit": "mph"
}
]
}
],
"forecast": [
{
"date": "2012-08-21",
"day": [
{
"weather_code": "0",
"weather_text": "Sunny skies",
"wind": [
{
"dir": "NW",
"dir_degree": "311",
"speed": "7",
"wind_unit": "mph"
}
]
}
],
"day_max_temp": "83",
"night": [
{
"weather_code": "0",
"weather_text": "Clear skies",
"wind": [
{
"dir": "WNW",
"dir_degree": "289",
"speed": "7",
"wind_unit": "mph"
}
]
}
],
"night_min_temp": "57",
"temp_unit": "f"
},
{
"date": "2012-08-22",
"day": [
{
"weather_code": "0",
"weather_text": "Sunny skies",
"wind": [
{
"dir": "N",
"dir_degree": "7",
"speed": "4",
"wind_unit": "mph"
}
]
}
],
"day_max_temp": "85",
"night": [
{
"weather_code": "0",
"weather_text": "Clear skies",
"wind": [
{
"dir": "S",
"dir_degree": "176",
"speed": "7",
"wind_unit": "mph"
}
]
}
],
"night_min_temp": "63",
"temp_unit": "f"
}
]
}
};
On very simple json data, I can figure out how to get at the variables. On something like the above, it is difficult for me.
So to access say where the temp is 70, I would code:
var my_temp= weather.curren_weather.temp // This does not work
So just as 2 examples how would I get at the temp(70) and say where the wind_degree is 311?
Also is there a program where you can paste in the json object like the above and it will output the correct code needed to access any variable?
Thanks,
Jim
Your object is complicated by the nested arrays, and it’s unclear why current weather requires a nested array.
To get at the
tempof 70 given this structure, you’ll need:And for that particular wind reading:
For the
forecastfield, the nested array is reasonable, since you’re given a list of forecasts for different datesm and so the daytime wind reading for the second date represented in your data can be extracted using: