JSON解析

这篇文章先来看看JSON的数据解析。

JSON是一种轻量级的数据格式,一般用于数据交互。

首先了解JSON底层做了些什么事情:遍历字符串中的字符,最终根据格式规定的特殊字符,将JSON数据转化为字典,字典中的值可能是字典、数组、字符串等。

JSON - OC转换对照表

JSON OC
{} NSDictionary
[] NSArray
"" NSString
数字(10, 10.5) NSNumber
true NSNumber(@1)
false NSNumber(@0)
null NSNull

示例代码:

    // 1.请求路径
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=JSON"];
    // 2.创建请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 3.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        // 解析JSON
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        NSLog(@"%@", dict);
    }];

注:报错的情况可能没有设置info.plist中的http安全协议

注:以上代码运行在iOS9以上,提示代码过时的警告,则使用以下代码

示例代码:

    // 1.请求路径
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=JSON"];
    // 2.创建对象
    NSURLSession *session = [NSURLSession sharedSession];
    // 3.创建任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 解析JSON
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        NSLog(@"%@", dict);
    }];
    
    // 4.启动任务
    [dataTask resume];

利用cocoaPods管理第三方框架(SDWebImage, MJExtension)

将字典数组转换为模型数组,显示到每一个cell上,使用MediaPlayer播放视频

示例代码

 - (void)viewDidLoad {
    [super viewDidLoad];
    // 1.请求路径
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=JSON"];
    // 2.创建对象
    NSURLSession *session = [NSURLSession sharedSession];
    // 3.创建任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 解析JSON
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        
        self.array = [WXModel mj_objectArrayWithKeyValuesArray:dict[@"videos"]];
        
        [self.tableView reloadData];
    }];
    
    // 4.启动任务
    [dataTask resume];
}

#pragma mark - <数据源方法>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    WXModel *model = self.array[indexPath.row];
    
    NSString *image = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:model.image];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"Snip20151216_4"]];
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%zd", model.length];
    return cell;
}

#pragma mark - <代理方法>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WXModel *model = self.array[indexPath.row];
    NSString *urlStr = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:model.url];
    //创建视频播放器
    MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlStr]];
    
    [self presentViewController:vc animated:YES completion:nil];
  
}
原文地址:https://www.cnblogs.com/sjxjjx/p/5050876.html