I am working with the Forecastr wrapper library (https://github.com/iwasrobbed/Forecastr) to grab data from the website Forecast.io. Currently in the example project provided in the github project, the output spits out:
{
currently = {
apparentTemperature = "68.72";
cloudCover = 1;
dewPoint = "66.43000000000001";
humidity = "0.92";
icon = cloudy;
nearestStormDistance = 0;
ozone = 327;
precipIntensity = "0.0021";
precipIntensityError = "0.0002";
precipProbability = "0.01";
precipType = rain;
pressure = "1011.38";
summary = "Humid and Overcast";
temperature = "68.72";
time = 1403645561;
visibility = "4.62";
windBearing = 182;
windSpeed = "4.86";
};
daily = {
data = (
{
apparentTemperatureMax = "70.81999999999999";
apparentTemperatureMaxTime = 1403658000;
apparentTemperatureMin = "64.78";
apparentTemperatureMinTime = 1403596800;
cloudCover = "0.9399999999999999";
dewPoint = "63.8";
humidity = "0.86";
icon = rain;
moonPhase = "0.92";
ozone = "328.74";
precipIntensity = "0.0286";
precipIntensityMax = "0.0834";
precipIntensityMaxTime = 1403636400;
precipProbability = 1;
precipType = rain;
pressure = "1013.38";
summary = "Rain throughout the day.";
sunriseTime = 1403600850;
sunsetTime = 1403657295;
temperatureMax = "70.81999999999999";
temperatureMaxTime = 1403658000;
temperatureMin = "64.78";
temperatureMinTime = 1403596800;
time = 1403582400;
visibility = "7.24";
windBearing = 169;
windSpeed = "3.97";
},
{
apparentTemperatureMax = "75.17";
apparentTemperatureMaxTime = 1403726400;
apparentTemperatureMin = "65.19";
apparentTemperatureMinTime = 1403694000;
cloudCover = "0.99";
dewPoint = "66.65000000000001";
humidity = "0.89";
icon = rain;
moonPhase = "0.95";
ozone = "323.68";
precipIntensity = "0.0362";
precipIntensityMax = "0.1194";
precipIntensityMaxTime = 1403668800;
precipProbability = "0.9399999999999999";
precipType = rain;
pressure = "1011.43";
summary = "Rain in the morning and afternoon.";
sunriseTime = 1403687270;
sunsetTime = 1403743699;
temperatureMax = "75.17";
temperatureMaxTime = 1403726400;
temperatureMin = "65.19";
temperatureMinTime = 1403694000;
time = 1403668800;
visibility = "7.69";
windBearing = 14;
windSpeed = "5.46";
},
{
apparentTemperatureMax = "70.76000000000001";
apparentTemperatureMaxTime = 1403805600;
apparentTemperatureMin = "62.47";
apparentTemperatureMinTime = 1403838000;
cloudCover = "0.79";
dewPoint = "62.15";
humidity = "0.86";
icon = rain;
moonPhase = "0.98";
ozone = "340.41";
precipIntensity = "0.0134";
precipIntensityMax = "0.0398";
precipIntensityMaxTime = 1403762400;
precipProbability = "0.91";
precipType = rain;
pressure = "1011.22";
summary = "Light rain until afternoon.";
sunriseTime = 1403773692;
sunsetTime = 1403830101;
temperatureMax = "70.76000000000001";
temperatureMaxTime = 1403805600;
temperatureMin = "62.47";
temperatureMinTime = 1403838000;
time = 1403755200;
visibility = "9.67";
windBearing = 4;
windSpeed = "10.54";
},
{
apparentTemperatureMax = "77.7";
apparentTemperatureMaxTime = 1403899200;
apparentTemperatureMin = "56.96";
apparentTemperatureMinTime = 1403856000;
cloudCover = "0.15";
dewPoint = "58.31";
humidity = "0.73";
icon = "partly-cloudy-day";
moonPhase = "0.02";
ozone = "354.38";
precipIntensity = "0.0007";
precipIntensityMax = "0.0021";
precipIntensityMaxTime = 1403841600;
precipProbability = "0.14";
precipType = rain;
pressure = "1016.58";
summary = "Partly cloudy starting in the afternoon, continuing until evening.";
sunriseTime = 1403860116;
sunsetTime = 1403916500;
temperatureMax = "77.7";
temperatureMaxTime = 1403899200;
temperatureMin = "56.96";
temperatureMinTime = 1403856000;
time = 1403841600;
visibility = 10;
windBearing = 332;
windSpeed = "3.48";
},
{
apparentTemperatureMax = "82.34999999999999";
apparentTemperatureMaxTime = 1403985600;
apparentTemperatureMin = "58.31";
apparentTemperatureMinTime = 1403946000;
cloudCover = "0.14";
dewPoint = "61.68";
humidity = "0.76";
icon = "partly-cloudy-day";
moonPhase = "0.05";
ozone = "346.75";
precipIntensity = "0.0002";
precipIntensityMax = "0.0004";
precipIntensityMaxTime = 1403996400;
precipProbability = "0.02";
precipType = rain;
pressure = "1020.75";
summary = "Partly cloudy in the afternoon.";
sunriseTime = 1403946543;
sunsetTime = 1404002897;
temperatureMax = "80.54000000000001";
temperatureMaxTime = 1403985600;
temperatureMin = "58.31";
temperatureMinTime = 1403946000;
time = 1403928000;
windBearing = 230;
windSpeed = "1.58";
},
{
apparentTemperatureMax = "87.03";
apparentTemperatureMaxTime = 1404075600;
apparentTemperatureMin = "60.66";
apparentTemperatureMinTime = 1404028800;
cloudCover = "0.4";
dewPoint = "64.09999999999999";
humidity = "0.75";
icon = "partly-cloudy-day";
moonPhase = "0.08";
ozone = "320.44";
precipIntensity = "0.0001";
precipIntensityMax = "0.0002";
precipIntensityMaxTime = 1404068400;
precipProbability = "0.03";
precipType = rain;
pressure = "1019.06";
summary = "Partly cloudy throughout the day.";
sunriseTime = 1404032971;
sunsetTime = 1404089291;
temperatureMax = "83.62";
temperatureMaxTime = 1404075600;
temperatureMin = "60.66";
temperatureMinTime = 1404028800;
time = 1404014400;
windBearing = 193;
windSpeed = "2.76";
},
{
apparentTemperatureMax = "91.87";
apparentTemperatureMaxTime = 1404158400;
apparentTemperatureMin = "67.73999999999999";
apparentTemperatureMinTime = 1404118800;
cloudCover = "0.27";
dewPoint = "69.93000000000001";
humidity = "0.8";
icon = "partly-cloudy-day";
moonPhase = "0.11";
ozone = "307.38";
precipIntensity = "0.0013";
precipIntensityMax = "0.0039";
precipIntensityMaxTime = 1404180000;
precipProbability = "0.29";
precipType = rain;
pressure = "1012.98";
summary = "Partly cloudy starting in the afternoon.";
sunriseTime = 1404119401;
sunsetTime = 1404175684;
temperatureMax = "85.01000000000001";
temperatureMaxTime = 1404158400;
temperatureMin = "67.73999999999999";
temperatureMinTime = 1404118800;
time = 1404100800;
windBearing = 224;
windSpeed = "5.24";
},
{
apparentTemperatureMax = "93.66";
apparentTemperatureMaxTime = 1404244800;
apparentTemperatureMin = "71.77";
apparentTemperatureMinTime = 1404205200;
cloudCover = "0.13";
dewPoint = "73.33";
humidity = "0.85";
icon = rain;
moonPhase = "0.13";
ozone = "307.25";
precipIntensity = "0.0095";
precipIntensityMax = "0.0179";
precipIntensityMaxTime = 1404237600;
precipProbability = "0.64";
precipType = rain;
pressure = "1007.95";
summary = "Light rain throughout the day.";
sunriseTime = 1404205833;
sunsetTime = 1404262073;
temperatureMax = "84.91";
temperatureMaxTime = 1404244800;
temperatureMin = "71.77";
temperatureMinTime = 1404205200;
time = 1404187200;
windBearing = 227;
windSpeed = "5.82";
}
);
icon = rain;
summary = "Rain throughout the week, with temperatures bottoming out at 71\U00b0F on Thursday.";
};
flags = {
"darksky-stations" = (
KCXX
);
"isd-stations" = (
"711830-99999",
"713710-99999",
"713713-99999",
"716120-99999",
"716275-99999"
);
"madis-stations" = (
AS947,
AU037,
BATEU06,
C3442,
C7533,
C8027,
C8480,
CWHM,
CWIZ,
CWTA,
CWVQ,
CYHU,
CYUL,
D2821,
D3464,
D8252
);
sources = (
nwspa,
isd,
"nearest-precip",
gfs,
fnmoc,
sref,
rtma,
rap,
nam,
cmc,
madis,
darksky
);
units = us;
};
offset = "-4";
timezone = "America/Montreal";
}
Now the library states this is JSON and this looks like JSON, but it actually is not JSON so I am unsure of how to go about parsing this NSString in my Xcode project.
SOLUTION:
It is a NSDictionary of type id. Thus to parse it as mvanallen states do this:
NSLog(@"Summary: %@",[json valueForKeyPath:@"currently.summary"]);
..should print out:
Summary: Humid and Overcast
Looking closer at the code there are certain externs such as:
extern NSString *const kFCCurrentlyForecast;
Therefore I can also do something like:
NSLog(@"%@",[[JSON objectForKey:kFCCurrentlyForecast] objectForKey:kFCApparentTemperature]);
{
character?) which represents some JSON. Go to json.org to see the JSON syntax (it's very simple to learn), then understand that JSON arrays are mapped to NSArray objects and JSON "objects" are mapped to NSDictionary objects. The appearance is mostly the same in both representations, only the JSON[]
for an array is replaced with()
for the NSArray, and the NSDictionary key/value separator is=
rather than:
. (And a few more minor differences.) So net-net is that what you have is already parsed.