iOS学习笔记25-Json数据解析

JSON数据实例

 

 

 

 

首先找到路径

 NSString *strUrl = @"http://localhost/Stu.json";

得到request

 NSURL *url = [NSURL URLWithString:strUrl];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSURLResponse *response = nil;

    NSError *error = nil;

 

得到data

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

 

//定义解析 Json 的对象

    NSMutableArray *arr =  [NSJSONSerialization JSONObjectWithData:data

                                    options:kNilOptions

                                      error:&error];

    NSLog(@"%@",arr);

    //字典转模型

    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count];

    for(NSDictionary *dic in arr){

        NSLog(@"%@",dic);

        Stu *stu = [[Stu alloc]initWithDictionary:dic];

        NSLog(@"stu = %@",stu);

        [arrM addObject:stu];

    }

    for (int i =0; i<arrM.count; i++) {

        NSLog(@"stu = %@",arrM[i]);

    }

 

 

 

 

运行结果

 

 

posted @ 2016-02-26 14:20  small-elephant_A-Do  阅读(147)  评论(0编辑  收藏  举报