zoukankan      html  css  js  c++  java
  • 加载JSON文件,Plist文件

    1.加载JSON文件

    a.JSON文件的路径

     NSString *path = [[NSBundle mainBundle] pathForResource:@"XXXX.json" ofType:nil];

    b.加载JSON文件  获取Data数据 

     NSData *data = [NSData dataWithContentsOfFile:path];

    c.将JSON数据转为NSArray或者NSDictionary

    NSArray *dictArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    d.将字典转成模型

     NSMutableArray *array= [NSMutableArray array];
            for (NSDictionary *dict in dictArray) {
                XX自己的模型 *p = [XX自己的模型 xxWithDict:dict];
                [array addObject:p];
            }
           return array;
    2.加载Plist文件

    a.获得plist的全路径
            NSString *path = [[NSBundle mainBundle] pathForResource:@"XXX.plist" ofType:nil];
            
           b.加载数组
            NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
            
            c.将dictArray里面的所有字典转成模型对象,放到新的数组中
            NSMutableArray *gameArray = [NSMutableArray array];
            for (NSDictionary *dict in dictArray) {
                // 3.1.创建模型对象
                QsGame *game = [QsGame gameWithDict:dict];
                // 3.2.添加模型对象到数组中
                [gameArray addObject:game];
            }
            // 4.赋值
            _games = gameArray;
    3.读取沙河中的plist

       NSString *home = NSHomeDirectory(); // 获得沙盒根路径
         NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 拼接document路径
         NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"]; //  获得文件路径
         NSArray *data = [NSArray arrayWithContentsOfFile:filepath];  // 开始读取数据
         NSLog(@"%@", data);

  • 相关阅读:
    JavaScript异步编程1——Promise的初步使用
    Pailler
    ElGamal
    RSA
    密码基础
    博客园中:为文章添加版权保护
    DCT实现水印嵌入与提取(带攻击)
    量子:基于EPR块对的两步量子直接通信
    量子:拜占庭协议和测谎问题的量子协议的实验证明
    liunx:网络命令
  • 原文地址:https://www.cnblogs.com/qingsongeasy/p/3720776.html
Copyright © 2011-2022 走看看