jesonModel 的简单使用

这里给出了大致的思路 只是个基础 大家还要多使用 
这个库需要自己下载并引入 即可使用
#import "ViewController.h"
#import "itemModel.h"

@interface ViewController ()
/**
 *      NSString* urlStr1 = @"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=3";
 */
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString* urlStr = @"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=3";
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    [self downloadTask:url];
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)downloadTask:(NSURL *)url{
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //取到最外层字典
        NSDictionary *dictjeson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //取到字典里面的applications 数据
        NSArray *applications = [dictjeson objectForKey:@"applications"];
        for(NSDictionary *dict in applications)
        {
            itemModel *item = [[itemModel alloc]initWithDictionary:dict error:nil];
            NSLog(@"item:%@",item);
            NSLog(@"%@",item.toJSONString);
        }
    }];

    //任务执行
    [dataTask resume];
    NSString *json = @"{\"applicationId\": \"757752585\",\"slug\": \"puzzle-chef\",\"name\": \"美味厨房\",\"releaseDate\": \"2014-09-17\",\"version\": \"1.3.0\"}";
    
    //能够自动检测key 的正确性
    itemModel *model = [[itemModel alloc]initWithString:json usingEncoding:NSUTF8StringEncoding error:nil];
    
    NSLog(@"------------model-------------%@",model);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(网络,数据,下载,ios开发,库)