简单的demo:
NSError *error;
//1. 加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101100101.html"]];
//2. 将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//3. iOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
//4. weatherDic字典中存放的数据也是字典型,从它里面通过键值取值
NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
NSLog(@"今天%@的天气是%@ 最高温度是:%@ 最低温度是:%@",[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather"],[weatherInfo objectForKey:@"temp1"],[weatherInfo objectForKey:@"temp2"]);
整个过程就是发送请求→接受请求 病存到字典中,然后就是从字典中取数据了。