在UILabel中显示JSON信息

时间:2013-09-06 16:14:13

标签: ios json weather-api

我从世界天气在线获取JSON,我试图在标签中显示华氏温度,但是当我运行它时,该标签显示(null)。 JSON在控制台中显示正常,所以我知道它正在提取正确的数据。有什么想法吗?

#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

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;
        }
    );
}

我试图用我发布的代码来完成这项工作。如果我使用天气数组,应用程序会在NSDictionary *weatherConditions = [weather objectAtIndex:0];[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa158f70

崩溃

更新:我修复了它为temp_F返回NULL的问题。

以下是当前代码:

- (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]];
}

我现在唯一的问题是weatherIconUrl现在返回NULL并且我不确定原因。

1 个答案:

答案 0 :(得分:0)

我明白了。这是代码。

- (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]];
}