IOS中JSON解析<代码演示>

    /*

     JSON文件使用键值对KeyValue的形式出现的

     

     IOS开发中使用JSON数据较多,一般使用的都是JSON解析数据比如一下一份JSON文件:

     {

         "students": [

         {

             "name":"张三",

             "telephoneNumber":"1234567891",

             "gender":""

         },

         {

             "name":"李四",

             "telephoneNumber":"1734995333",

             "gender":""

         }

         ]

     }

    解析步骤很简单单:

     1.获取沙盒中JSON文件<说明:这里演示的是沙盒下的JSON解析,开发中通常使用的网络数据>

     2.调用JSON解析器直接解析路径下的JSON文件存入字典中<Java中的Map集合>

     */

    

    

    /*-----------------使用JSON解析------------------*/

    //1.获取JSON文件

    NSString *jsonString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"students" ofType:@"json"]encoding:NSUTF8StringEncoding error:nil];

    //2.实例化JSON解析器

    NSDictionary *jsonSerial = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];

    

    NSArray *value = [jsonSerial objectForKey:@"students"];

    self.stuArray = [NSMutableArray array];

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

        NSDictionary *dic = [value objectAtIndex:i];

        Students *stus = [[Students alloc] init];

        stus.name = [dic objectForKey:@"name"];

        stus.telephoneNumber = [dic objectForKey:@"telephoneNumber"];

        stus.gender = [dic objectForKey:@"gender"];

        [self.stuArray addObject:stus];

        [stus release],stus = nil;

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GFanStudio-LeeSir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值