There is no current_weather
key in the JSON data you posted. The structure is:
{ "data": { "current_condition": [ { ..., "temp_C": "7", ... } ], ... } }
Here's a visual representation:
Therefore, to get to temp_C
, you'd need to first obtain the top-level data
property:
NSDictionary* json = (NSDictionary*)[responseString JSONValue];
NSDictionary* data = [json objectForKey:@"data"];
then, from that, obtain the current_location
property:
NSArray* current_condition = [data objectForKey:@"current_condition"];
and finally, from the current_location
array, get the element you're interested in:
NSDictionary* weathernow = [current_condition objectAtIndex:0];
Also note that temp_C
and weatherCode
are strings, not numbers. To transform them to numbers, instead of:
NSNumber* tempc = [weathernow objectForKey:@"temp_C"];
NSNumber* weatherCode = [weathernow objectForKey:@"weatherCode"];
you could use something like:
int tempc = [[weathernow objectForKey:@"temp_C"] intValue];
int weatherCode = [[weathernow objectForKey:@"weatherCode"] intValue];
(or floatValue
/ doubleValue
if the value is not supposed to be an int
, but rather a float
or a double
)
You would then use %d
(or %f
for float
/ double
) as a format string:
NSLog(@"%d %d", tempc, weatherCode);