美文网首页
JsonModel使用

JsonModel使用

作者: Ghstart | 来源:发表于2014-03-02 09:00 被阅读735次
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        customTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        customTableView.delegate = self;
        customTableView.dataSource = self;
        [self.view addSubview:customTableView];
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated
{
    //1.
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


        //2.
        NSData *ghData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://api.github.com/users/icanzilb"]];
   

        //3.
        NSDictionary *json = nil;

        if (ghData) {
            json = [NSJSONSerialization JSONObjectWithData:ghData options:NSJSONReadingMutableContainers error:nil];
        }


        //4.
        dispatch_async(dispatch_get_main_queue(), ^{

            //在主线程执行的代码
            user = [[MainModel alloc] initWithDictionary:json error:NULL];
            items = @[user.login,user.html_url,user.company,user.name,user.blog];
            [customTableView reloadData];
        });
    });
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [customTableView deselectRowAtIndexPath:indexPath animated:YES];

    if ([items[indexPath.row] isKindOfClass:[NSURL class]]) {//判断是为这个类或者为这个子类的实例   http://blog.csdn.net/totogo2010/article/details/7714960
        [[UIApplication sharedApplication] openURL:items[indexPath.row]];//加载其他的应用程序  http://blog.csdn.net/joe1209/article/details/9931381
    }
}




2.建立一个Model类型继承JSONModel,并且字段要和JSON的对应起来。OR

+(JSONKeyMapper *)keyMapper
{
    return [[JSONKeyMapper alloc] initWithDictionary:@{
                                                       @"login":@"login1",
                                                       @"html_url":@"html_url1",
                                                       @"company":@"company1",
                                                       @"name":@"name1",
                                                       @"blog":@"blog1"
                                                       }];
}

[http://www.cnblogs.com/biosli/archive/2013/05/20/jsonmodel%E6%BA%90%E7%A...]

相关文章

网友评论

      本文标题:JsonModel使用

      本文链接:https://www.haomeiwen.com/subject/bfahtttx.html