• 源码0602-01-掌握-JSON解析


    //  ViewController.m
    //  01-掌握-JSON解析
    #import "ViewController.h"
    #import <UIImageView+WebCache.h>
    #import <MediaPlayer/MediaPlayer.h>
    
    @interface ViewController ()
    /** 视频数据 */
    @property (nonatomic, strong) NSArray *videos;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    //    NSString *json = @"{"rich" : null}";
        NSString *json = @"10";
        NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil]);
        
        
        // 0.请求路径
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video"];
        
        // 1.创建请求对象
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        // 2.发送请求
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            // 解析JSON
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            
            // 获得视频数组
            self.videos = dict[@"videos"];
            
            // 刷新表格
            [self.tableView reloadData];
            //[dict writeToFile:@"/Users/xiaomage/Desktop/video.plist" atomically:YES];//将json数据写入plist文件查看其格式内容;
        }];
    }
    
    // JSON格式化:http://tool.oschina.net/codeformat/json
    - (void)parseJSON
    {
        // 0.请求路径
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520000&pwd=520it"];
        
        // 1.创建请求对象
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        // 2.发送请求
        [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            /*
             NSJSONReadingMutableContainers
             NSJSONReadingMutableLeaves
             NSJSONReadingAllowFragments //碎片式读取
             */
            // 解析JSON
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            
            NSLog(@"%@", dict[@"error"]);
        }];
    }
    
    #pragma mark - 数据源方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.videos.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID = @"video";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        NSDictionary *video = self.videos[indexPath.row];
        
        cell.textLabel.text = video[@"name"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"时长:%@", video[@"length"]];//字符串--->数值 -转化
        
        NSString *image = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video[@"image"]]; //路径字符串拼接
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        return cell;
    }
    
    #pragma mark - 代理方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary *video = self.videos[indexPath.row];
        NSString *urlStr = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video[@"url"]];
        
        // 创建视频播放器
        MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlStr]];
    
        // 显示视频
        [self presentViewController:vc animated:YES completion:nil];
    }
    
    @end

    02-掌握-JSON解析

    MJExtension 插件  json解析 牛牛牛
    //  ViewController.m
    //  01-掌握-JSON解析
    #import "ViewController.h"
    #import <UIImageView+WebCache.h>
    #import <MediaPlayer/MediaPlayer.h>
    #import "XMGVideo.h"
    #import <MJExtension.h>
    
    @interface ViewController ()
    /** 视频数据 */
    @property (nonatomic, strong) NSArray *videos;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [XMGVideo setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{@"ID" : @"id"};
        }];
        
        // 0.请求路径
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video"];
        
        // 1.创建请求对象
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        // 2.发送请求
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            // 解析JSON
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            
            // 获得视频的模型数组
            self.videos = [XMGVideo objectArrayWithKeyValuesArray:dict[@"videos"]];
            
            // 刷新表格
            [self.tableView reloadData];
        }];
    }
    
    #pragma mark - 数据源方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.videos.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID = @"video";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        XMGVideo *video = self.videos[indexPath.row];
        
        cell.textLabel.text = video.name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"时长:%zd", video.length];
        
        NSString *image = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.image];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        return cell;
    }
    
    #pragma mark - 代理方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        XMGVideo *video = self.videos[indexPath.row];
        NSString *urlStr = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.url];
        
        // 创建视频播放器
        MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlStr]];
    
        // 显示视频
        [self presentViewController:vc animated:YES completion:nil];
    }
    
    @end
    //
    //  XMGVideo.h
    //  01-掌握-JSON解析
    #import <Foundation/Foundation.h>
    
    @interface XMGVideo : NSObject
    /** ID */
    @property (nonatomic, assign) NSInteger ID;
    /** 视频名字 */
    @property (nonatomic, copy) NSString *name;
    /** 视频时长 */
    @property (nonatomic, assign) NSInteger length;
    /** 视频图片 */
    @property (nonatomic, copy) NSString *image;
    /** 视频文件路径 */
    @property (nonatomic, copy) NSString *url;
    @end
    //
    //  XMGVideo.m
    //  01-掌握-JSON解析
    #import "XMGVideo.h"
    
    @implementation XMGVideo
    //+ (NSDictionary *)replacedKeyFromPropertyName
    //{
    //    return @{@"ID" : @"id",
    //             @"desc" : @"description"};
    //}
    @end
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    超酷震撼 HTML5/CSS3动画应用及源码
    精美jQuery插件及源码 前端开发福利
    web前端炫酷实用的HTML5应用和jQuery插件
    8个web前端的精美HTML5 & CSS3效果及源码下载
    分享web前端七款HTML5 Loading动画特效集锦
    7款超酷HTML5 3D动画精选应用及源码
    8个WEB前端创意HTML5动画应用精选
    让人心动的jQuery插件和HTML5动画
    华丽的HTML5/jQuery动画和应用 前端必备
    炫酷实用的jQuery插件 涵盖菜单、按钮、图片
  • 原文地址:https://www.cnblogs.com/laugh/p/6588990.html
Copyright © 2020-2023  润新知