iOS 全国天气预报json解析

tableviewController.m文件

1.@interface rootTableViewController ()
@property(nonatomic,strong)NSMutableArray *arrM;
@end

@implementation rootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.arrM=[[NSMutableArray alloc]init];

}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillDisappear:YES];
WeatherJson *wjson=[[WeatherJson alloc]init];
self.arrM=[wjson jsonUrlweather];
[self.tableView reloadData];
if (self.arrM==nil) {
    NSLog(@"正在刷新");
}
else
{
    NSLog(@"刷新成功");
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection
(NSInteger)section {
return self.arrM.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *temdic=self.arrM[indexPath.row];
NSString *info=@"MyTableCell";
MyTableCell *cell=[tableView dequeueReusableCellWithIdentifier:info];
if (cell==nil) {
    Class cls=NSClassFromString(info);
    cell=[[cls alloc]initWithStyle:UITableViewCellStyleDefault
    reuseIdentifier:info];
}
[cell Mytablecell:temdic];
return cell;
}

自定义tableview

1.MytableCell.h文件
@interface MyTableCell : UITableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
-(void)Mytablecell:(NSDictionary *)dic;
@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *label2;
@property(nonatomic,strong)UILabel *label3;
@property(nonatomic,strong)UILabel *label4;
@end

MytableCell.m文件

1.@implementation MyTableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    self.label1=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 80, self.frame.size.height)];
    self.label1.textColor=[UIColor colorWithRed:0.089 green:0.240 blue:1.000 alpha:1.000];
    [self addSubview:self.label1];
    
    self.label2=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 280, self.frame.size.height)];
    self.label2.textColor=[UIColor colorWithRed:1.000 green:0.067 blue:0.062 alpha:1.000];
    [self addSubview:self.label2];
    
    self.label3=[[UILabel alloc]initWithFrame:CGRectMake(360, 0, 54, self.frame.size.height)];
    self.label3.textColor=[UIColor colorWithRed:0.332 green:1.000 blue:0.030 alpha:1.000];
    [self addSubview:self.label3];
}
return self;
}
iOS 全国天气预报json解析_第1张图片
屏幕快照 2016-03-24 下午10.10.52.png
将解析出来的数据通过key值赋给tableview上
-(void)Mytablecell:(NSDictionary *)dic
{
self.label1.text=dic[@"weatherinfo"][@"city"];
self.label2.text=dic[@"weatherinfo"][@"weather"];
self.label3.text=dic[@"weatherinfo"][@"ptime"];
}
@end

对网络请求下来的天气进行解析

1.WeatherJson.h文件
@interface WeatherJson : NSObject
@property(nonatomic,strong)NSMutableArray *arrM;
-(NSMutableArray *)jsonUrlweather;
@end
2.WeatherJson.m文件
@implementation WeatherJson
-(NSMutableArray *)jsonUrlweather
{
NSMutableArray *arrM1=[NSMutableArray array];
self.arrM=[NSMutableArray array];
这里是将全国省会的天气预报从网上查到相应的网站一一列表出来  
for (int i=1; i<=34; i++) {
    if (i<5) {
        NSString *path=[NSString stringWithFormat:@"http://www.weather.com.cn/data/cityinfo/101%02d0100.html",i];
        [self.arrM addObject:path];
    }else
    {
       NSString *path=[NSString stringWithFormat:@"http://www.weather.com.cn/data/cityinfo/101%02d0101.html",i];
        [self.arrM addObject:path];
    }
}        
网络请求数据
for (int i=0; i<34; i++) {
    NSURL *url=[NSURL URLWithString:self.arrM[i]];
    NSData *data=[[NSData alloc]initWithContentsOfURL:url];
    解析
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@  天气%@ 发布时间%@",dic[@"weatherinfo"][@"city"],dic[@"weatherinfo"][@"weather"],dic[@"weatherinfo"][@"ptime"]);
    [arrM1 addObject:dic];
}
return arrM1;
}
@end
iOS 全国天气预报json解析_第2张图片
屏幕快照 2016-03-27 下午7.34.36.png

你可能感兴趣的:(iOS 全国天气预报json解析)