zoukankan      html  css  js  c++  java
  • 解析数据的步骤

    解析数据的步骤

    1、plist文件数据

        //获取文件路径

        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];

        

        //从文件路径中提取数组

        NSArray *array = [NSArray arrayWithContentsOfFile:filePath];

        

        //初始化数据数组

        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];

        

        //遍历数组,进行添加模型

        for (NSDictionary *dic in array) {

            Student *student = [[Student alloc] init];

            [student setValuesForKeysWithDictionary:dic];

            [_dataArray addObject:student];

            [student release];

        }

    2、解析JSON数据

        //获取json数据的路径

        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"json"];

        

        //获取NSData对象

        NSData *data = [NSData dataWithContentsOfFile:filePath];

        

        //解析JSON数据

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

        

        //初始化模型数组

        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];

        

        for (NSDictionary *dic in array) {

            Model *model = [[Model alloc] init];

            [model setValuesForKeysWithDictionary:dic];

            [_dataArray addObject:model];

            [model release];

        }

    从上面两个例子可以看出来,解析数据的步骤大概可以分为以下几步:

    1、获取数据的路径

    2、从文件路径中提取对应的数据类型

    3、解析数据

    (1)初始化模型数组

    (2)解析的最终结果都是将字典转换成模型,所以我们要理清层次关系,明白字典是有键—值对组成的。

    (3)将模型加到事先声明的字典或者数组中。

  • 相关阅读:
    Window对象与DOM
    redis在linux环境下的安装与启动
    分布式,集群与负载平衡是什么?
    Hadoop之hive安装过程以及运行常见问题
    Hadoop之Pig安装
    eclipse安装Hadoop1.1.2版本开发插件
    linux ant 解决 错误: 找不到或无法加载主类 org.apache.tools.ant.launch.Launcher
    运行.class文件提示找不到或者无法加载主类原因
    bzoj4337: BJOI2015 树的同构
    bzoj1500: [NOI2005]维修数列
  • 原文地址:https://www.cnblogs.com/d-mm/p/5210222.html
Copyright © 2011-2022 走看看