本例的目的:从网站请求了天气信息,信息以Json的格式返回,通过解析数据把有关信息显示在界面
遇到的问题在于:把Json数据转换到NSDictionary类型中,通过键值来获取特定数据,将数据赋给label显示,但在编译时会出错
错误提示: [__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x792ab510
错误解析:网上有一种说法是“由于对象被提前释放了,但在程序中仍然调用了该对象”,我试了这种错误的解决办法,但上面的错误提示仍然出现,个人认为并不是这里的错误,我是采用ARC模式编程,我的对象是在同一个方法中的,所以不会存在提前释放的可能。
后来看到另一种说法,类型不匹配!最后证实,确实是因为类型的不匹配。之所以会这样,就是因为NSDictionary对象中存储的数据的类型并不是统一的,仍然是以获取到的Json数据的数据类型为基础的。换句话说数据存储在字典中,并没有改变数据类型。
获取到的Json数据:
JSONDictionary = {
base = stations;
clouds = {
all = 0;
};
cod = 200;
coord = {
lat = "23.12";
lon = "113.25";
};
dt = 1448256600;
id = 1809858;
main = {
humidity = 51;
pressure = 1016;
temp = "302.15";
"temp_max" ="302.15";
"temp_min" ="302.15";
};
name = Guangzhou;
sys = {
country = CN;
id = 7414;
message = "0.0131";
sunrise = 1448232336;
sunset = 1448271647;
type = 1;
};
visibility = 10000;
weather = (
{
description = "Sky isClear";
icon = 01d;
id = 800;
main = Clear;
}
);
wind = {
deg = 50;
speed = 3;
};
}
代码示例:
//转换方法,这里是把请求到的json数据转换成其他类型的数据
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
if (jsonObject != nil && error == nil){
NSLog ( @"Successfully deserialized..." );}
}
NSDictionary *main = [weatherDic objectForKey:@"main"];
// NSLog(@"main class:%@",[main class]);//main是NSDictionary类型
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 190, 100, 40)];
NSString *temp = [main objectForKey:@"temp"];
// NSLog(@"temp class:%@",[temp class]);//temp实际上是NSNumber类型
// tempLabel.text = temp;//由于temp的NSNumber类型与text的NSString类型不匹配,所以会报错,使用下面的方法进行转换
int tempInt = [temp intValue];//将开氏温度转变成摄氏温度,先将NSNumber转换成int
tempLabel.text = [NSString stringWithFormat:@"%d°C",(tempInt-273)];
[self.view addSubview:tempLabel];
UILabel *humLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 250, 100, 40)];
NSString *hum = [main objectForKey:@"humidity"];
// NSLog(@"hum:%@",[hum class]);//hum的NSNumber类型与text的NSString类型不匹配,所以要使用下面的方法进行转换
humLabel.text = [NSString stringWithFormat:@"%@",hum];
[self.view addSubview:humLabel];
UILabel *weatherLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 310, 100, 40)];
NSArray *weather = [weatherDic objectForKey:@"weather"];
// NSLog(@"%@",[weather class]);//NSArray
NSDictionary *weather0 = [weather objectAtIndex:0];
weatherLabel.text = [weather0 objectForKey:@"description"];
[self.view addSubview:weatherLabel];
UILabel *currentTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 70, 250, 40)];
NSNumber *UTC = [weatherDic objectForKey:@"dt"];
long long int date1 = (long long int)[UTC intValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:date1];//将utc秒数时间戳转换成一般的时间格式
// NSLog(@"时间戳转日期 %@ = %@", UTC, date);
currentTimeLabel.text = [NSString stringWithFormat:@"%@",[self getNowDateFromatAnDate:date]];
[self.view addSubview:currentTimeLabel];
UILabel *cityNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 130, 100, 40)];
NSString *name = [weatherDic objectForKey:@"name"];
cityNameLabel.text = name;
[self.view addSubview:cityNameLabel];