代碼:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self getWeatherInfo];
}
//獲得徐家匯的天氣預報
-(void)getWeatherInfo{
NSError *error;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101021200.html" ]];//代碼為徐家匯
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
NSLog(@"天氣%@",[NSString stringWithFormat:@"\n%@\n%@\n%@\n%@\n%@\n ", [weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]);
/*
天氣
徐家匯
2014年3月4日
星期二
小雨轉多雲
11℃~6℃
*/
}
輸出:
2015-03-11 16:02:06.686 徐家匯天氣預報[5526:214455] 天氣
徐家匯
2014年3月4日
星期二
小雨轉多雲
11℃~6℃