#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// NSURL(西安天气预报)
NSURL *url = [NSURL URLWithString:@"http://www.weather.com.cn/adat/sk/101110101.html"];
// 创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:15.f];
// 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSLog(@"%@",data);
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@",result);
NSLog(@"城市 = %@, 温度 = %@ , 风向 = %@, 湿度 = %@",result[@"weatherinfo"][@"city"],result[@"weatherinfo"][@"temp"],result[@"weatherinfo"][@"WD"],result[@"weatherinfo"][@"SD"]);
}];
}
@end
网路:JSON 解析(天气预报)
最新推荐文章于 2020-06-28 12:24:26 发布