本地html文件和从网站解析数据
-(NSArray *)htmls{
if (!_htmls) {
//从help.json加载数据
//1.获取json路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"help.json" ofType:nil];
//2.把json转成NSData
NSData *data = [NSData dataWithContentsOfFile:filePath];
//3.再把NSData转成数组
//json的序列化 "就是把数据转成字典/数组"
NSArray *helpArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//NSLog(@"%@",obj);
//4.遍历数组里字典,转成模型
NSMutableArray *htmlsM =[NSMutableArray array];
for (NSDictionary *dict in helpArr) {
CZHtmlPage *htmlPage = [CZHtmlPage htmlPageWithDict:dict];
[htmlsM addObject:htmlPage];
}
_htmls = htmlsM;
}
return _htmls;
}
-(void)getDateModalWithCityName:(NSString *)cityName
{
// ------ 把CityName转换成CityID
//从网站后台获取数据
NSURL * url = [NSURL URLWithString:@"http://wthrcdn.etouch.cn/weather_mini?city=%E5%8C%97%E4%BA%AC"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
//3.连接类 负责做网络链接请求
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
if (error==nil)
{
NSError * jsonErr = nil;
//json解码错误
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonErr];
if (jsonErr==nil)
{
//NSLog(@"class:%@ result -->%@",[result class],result);
NSDictionary * dict = (NSDictionary *)result;
// NSLog(@"%@",dict);
NSDictionary *bigDic = dict[@"data"];
// NSDictionary * dataDict = dict[@"data"];
// 字典 转成 模型,把模型对象 用学过的传值方式 传出去
self.dateModal=[DateModal dateModalWithDic:bigDic];
NSLog(@"%@",self.dateModal.city);
}
}
}
]resume];
}

浙公网安备 33010602011771号