AFK解析Json的使用-3-使用数据

来源:互联网 发布:js判断数字大于等于0 编辑:程序博客网 时间:2024/04/29 12:25

AFK解析Json的使用-3-

接下来,尝试在tableView中显示数据。首先,构建下图界面。

控制器为:
@interface TableVC : UITableViewController

.h中只有一个点击方法:

@interface TableVC : UITableViewController

- (IBAction)jsonTap:(id)sender;

@end


.m中的代码:

#import "TableVC.h"

#import "NSDictionary+weather.h"

#import "NSDictionary+weather_package.h"

static NSString *const BaseURLString = @"http://www.raywenderlich.com/downloads/weather_sample/";    //声明一个静态变量

@interface TableVC ()

@property(strong,nonatomic)NSDictionary *wDic;    //存储天气的json

@property(strong,nonatomic)NSArray *wKey;     //存储天气的key,只是为了打印

@end


- (IBAction)jsonTap:(id)sender

{

    NSString *weatherURL=

    [NSString stringWithFormat:@"%@weather.php?format=json",BaseURLString];

    //得到json的URL

    NSURL *url=[NSURL URLWithString:weatherURL];   //将字符串转为URL

    NSURLRequest *request=[NSURLRequest requestWithURL:url];

    //新建一个针对URL的请求

    AFJSONRequestOperation *operation=[AFJSONRequestOperationJSONRequestOperationWithRequest:request

       success:^(NSURLRequest *request,NSHTTPURLResponse *response,id json){

        self.wDic=(NSDictionary *)json;   //将json转换为字典并存储

           [self.tableView reloadData];     //读json之后更新tableView的数据

        }

         failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

          UIAlertView *av=[[UIAlertView alloc]initWithTitle:@"未下载" message: [NSStringstringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

       [av show];

    }];

    [operation start];

    //-------打印Json数据的代码-----------

    self.wKey=[self.wDic allKeys];

    [self.wDic printDic];

    //-------打印Json数据的代码-----------

}


关键的数据就是wDic, self.wDic=(NSDictionary *)json; 这句即将json数据本地化。打印出的见文章1.

观察json数据,实际上分为两部分,一部分是今日天气,一个是weather的未来天气。所以

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 2;  //分为两部分

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    if(!self.wDic)

        return 0;

    

    switch (section) {

        case 0:{

            return 1;  //第0部分是1行

        }

        case 1:{

            NSArray *upcomingWeather = [self.wDic upcomingWeather];

            return [upcomingWeather count];

        }

        default:

            return 0;

    }

}

如果是第1部分,那么应该是weather这个数组的数量,即有多少个未来的天气条目。那么看看 upcomingWeather:

-(NSArray *)upcomingWeather{

    NSDictionary *dict = [self objectForKey:@"data"];

    return [dict objectForKey:@"weather"];

}


cell的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *daysWeather;

    switch (indexPath.section) {

        case 0:{

            daysWeather = [self.wDic currentCondition];

            cell.detailTextLabel.text=@"今日";

            break;

        }

        case 1:{

            NSArray *upcomingWeather = [self.wDic upcomingWeather];

            daysWeather = [upcomingWeather objectAtIndex:indexPath.row];

            cell.detailTextLabel.text=[daysWeather getDate];

        }

        default:

            break;

    }

     

    cell.textLabel.text = [daysWeather weatherDescription];

    return cell;

}


如果是第0个部分,则用currentCondition:

-(NSDictionary *)currentCondition{

    NSDictionary *dict = [self objectForKey:@"data"];

    NSArray *ar = [dict objectForKey:@"current_condition"];

    return [ar objectAtIndex:0];

}

如图




如果是第1部分,则用upcomingWeather:

-(NSArray *)upcomingWeather{

    NSDictionary *dict = [self objectForKey:@"data"];

    return [dict objectForKey:@"weather"];

}


getDate方法:

-(NSString *)getDate

{

    NSString *dateString = [self objectForKey:@"date"];

    return dateString;

}


效果如图:


原创粉丝点击