Question

I am getting JSON from World Weather Online and I'm trying to show the temperature in Fahrenheit in a label but when I run it, that label shows (null). The JSON shows fine in the console so I know it is pulling the right data. Any ideas?

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define weatherURL [NSURL URLWithString: @"http://api.worldweatheronline.com/free/v1/weather.ashx?q=47129&format=json&num_of_days=5&date=today&key=37a5fj42xpyptvjgkhrx5rwu"]

#import "Weather.h"
#import "WeatherLocation.h"

@interface Weather ()

@end

@implementation Weather

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    dispatch_async(kBgQueue, ^{
        NSData *data = [NSData dataWithContentsOfURL:weatherURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedData:(NSData *)responseData
{
    // parse out the JSON data
    NSError * error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSArray *weather = [json objectForKey:@"data"];

    NSLog(@"weather: %@", weather);

    NSArray *temp = [json objectForKey:@"temp_F"];

    NSDictionary *weatherConditions = [temp objectAtIndex:0];

    NSString *tempF = [weatherConditions objectForKey:@"temp_F"];

    currentTemp.text = [NSString stringWithFormat:@"%@", tempF];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Result of NSLog(@"weather: %@", weather);

weather: {
    "current_condition" =     (
                {
            cloudcover = 50;
            humidity = 83;
            "observation_time" = "11:28 AM";
            precipMM = "0.0";
            pressure = 1016;
            "temp_C" = 22;
            "temp_F" = 72;
            visibility = 13;
            weatherCode = 116;
            weatherDesc =             (
                                {
                    value = "Partly Cloudy";
                }
            );
            weatherIconUrl =             (
                                {
                    value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png";
                }
            );
            winddir16Point = N;
            winddirDegree = 0;
            windspeedKmph = 0;
            windspeedMiles = 0;
        }
    );
    request =     (
                {
            query = 47129;
            type = Zipcode;
        }
    );
    weather =     (
                {
            date = "2013-09-09";
            precipMM = "0.0";
            tempMaxC = 35;
            tempMaxF = 95;
            tempMinC = 21;
            tempMinF = 69;
            weatherCode = 113;
            weatherDesc =             (
                                {
                    value = Sunny;
                }
            );
            weatherIconUrl =             (
                                {
                    value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png";
                }
            );
            winddir16Point = SSW;
            winddirDegree = 210;
            winddirection = SSW;
            windspeedKmph = 12;
            windspeedMiles = 7;
        }
    );
}

I was attempting to make this work with the code I posted. If I use the weather array, the app crashes on NSDictionary *weatherConditions = [weather objectAtIndex:0]; with [__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa158f70

UPDATE: I fixed the issue with it returning NULL for temp_F.

Here is the current code:

- (void)fetchedData:(NSData *)responseData
{
    // parse out the JSON data
    NSError * error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"JSON: %@", json);

    NSDictionary *data = [json objectForKey:@"data"];
    NSArray *currentConditions = [data objectForKey:@"current_condition"];
    NSDictionary *conditions = [currentConditions objectAtIndex:0];

    NSArray *icon = [data objectForKey:@"weatherIconUrl"];
    NSDictionary *weatherIcon = [icon objectAtIndex:0];

    NSString *tempF = [NSString stringWithFormat:@"%@\u00B0", [conditions objectForKey:@"temp_F"]];
    NSString *imageURL = [NSString stringWithFormat:@"%@", [weatherIcon objectForKey:@"value"]];
    NSURL *conditionIcon = [NSURL URLWithString:imageURL];
    NSData *imageData = [NSData dataWithContentsOfURL:conditionIcon];
    UIImage *conditionImage = [UIImage imageWithData:imageData];

    NSLog(@"Image URL: %@", imageURL);
    conditionsImage.image = conditionImage;

    NSLog(@"temp_F: %@", tempF);

    currentTemp.text = [NSString stringWithFormat:@"%@", tempF];
    [currentTemp setFont:[UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:72.0]];
}

The only problem I'm having now is that the weatherIconUrl is returning NULL now and I'm not sure why.

Was it helpful?

Solution

I got it. Here is the code.

- (void)fetchedData:(NSData *)responseData
{
    // parse out the JSON data
    NSError * error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"JSON: %@", json);

    NSDictionary *data = [json objectForKey:@"data"];
    NSArray *currentConditions = [data objectForKey:@"current_condition"];
    NSDictionary *conditions = [currentConditions objectAtIndex:0];

    NSArray *icon = [conditions objectForKey:@"weatherIconUrl"];
    NSDictionary *weatherIcon = [icon objectAtIndex:0];

    NSArray *weatherDesc = [conditions objectForKey:@"weatherDesc"];
    NSDictionary *description = [weatherDesc objectAtIndex:0];

    NSString *tempF = [NSString stringWithFormat:@"%@\u00B0", [conditions objectForKey:@"temp_F"]];
    NSString *desc = [NSString stringWithFormat:@"%@", [description objectForKey:@"value"]];
    NSString *imageURL = [NSString stringWithFormat:@"%@", [weatherIcon objectForKey:@"value"]];
    NSURL *conditionIcon = [NSURL URLWithString:imageURL];
    NSData *imageData = [NSData dataWithContentsOfURL:conditionIcon];
    UIImage *conditionImage = [UIImage imageWithData:imageData];

    NSLog(@"Image URL: %@", imageURL);
    conditionsImage.image = conditionImage;

    NSLog(@"temp_F: %@", tempF);

    currentTemp.text = [NSString stringWithFormat:@"%@", tempF];
    [currentTemp setFont:[UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:72.0]];
    weatherDescription.text = [NSString stringWithFormat:@"%@", desc];
    [weatherDescription setFont:[UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:36.0]];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top