iOS 一个简单的获取网络json 解析成模型数组

来源:互联网 发布:linux修改hostname 编辑:程序博客网 时间:2024/05/17 10:03
  1. 首先建立一个singleview工程,并在故事版中添加UITableView,连好数据源和代理。

  2. 在.h文件中添加数据源和代理方法,并且声明一个UITableView的变量,代码如下。

  3. ?
    1
    2
    3
    4
    5
    6
    7
    #import <UIKit/UIKit.h>
     
    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
     
    @property (weak, nonatomic) IBOutlet UITableView *tableview;
     
    @end
  4. 剩下的代码比较简单,我就直接贴代码了,已经在里面详细注释了。

  5. .m文件如下

  6. ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    #import "ViewController.h"
     
    @interface ViewController ()
    {
        NSMutableArray *_listarr;  //定义一个存放数据的容器。
    }
     
    @end
     
    @implementation ViewController
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
         
        //ios7新特性,这个请自行百度。
        [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
     
         
        NSError *error;
         
        //加载一个NSURL对象
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.touzhu.cn/ex_jc.aspx?state=0&ifgetclass=1"]];
         
        //将请求的url数据放到NSData对象中
        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
         
        //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
         
        //获取数组list的内容
        NSArray *list = [dic objectForKey:@"list"];
        NSLog(@"list数组的内容为--》%@", list );
         
        //初始化成员变量
        _listarr = [[NSMutableArray alloc]init];
         
        //遍历数组list里的内容
        for (int i = 0; i<[list count]; i++) {
             
            //按数组中的索引取出对应的字典
            NSDictionary *listdic = [list objectAtIndex:i];
             
            //通过字典中的key取出对应value,并且强制转化为NSString类型
             NSString *teamname = (NSString *)[listdic objectForKey:@"name_j"];
             
            //将获取的value值放到数组容器中
            [_listarr addObject:teamname];
            NSLog(@"name内容为--》%@", teamname );
        }
         
    }
     
     
     
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        //必须返回与数据容器一样的数量,否则会报数据越界错
        return [_listarr count];
    }
     
     
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
         
        //将要显示的数据赋值到cell上
        cell.textLabel.text = [_listarr objectAtIndex:indexPath.row];
        return cell;
    }
     
     
    @end

7.    程序运行如下

         

0 0
原创粉丝点击