zoukankan      html  css  js  c++  java
  • JSON数据解析


        //Json : JavaScript Object Notation, 脚本对象标注法
    Student.json
    [
     {
        "age":"18",
        "name":"张三",
        "sex":""
     },
     {
       "age":"38",
       "name":"李四",
       "sex":""
     },
     {
        "age":"28",
        "name":"王五",
        "sex":""
     }
    ]
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.studentArray = [NSMutableArray arrayWithCapacity:0];
     (1)
            //获取文件路径
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];
            //转成NSData
        NSData *data = [NSData dataWithContentsOfFile:filePath];
            //进行json解析
        NSError *error = nil;
        
    //        NSJSONSerialization: iOS提供的json解析类
        
    //    NSJSONReadingMutableContainers: 解析到得字典和数组是可变的(NSMutabelArrary, NSMutableDictionary)
    //    NSJSONReadingMutableLeaves: 解析到得叶子节点的内容是可变的(NSMutableString)
    //    NSJSONReadingAllowFragments : 允许json数据的最外层不是字典和数据
        
        
        NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
        if (error) {
            NSLog(@"%@", error);
        }
        
        NSLog(@"%@", dataArray)
    for (NSDictionary *dic in dataArray) {
            Student *student = [[Student alloc] init];
            student.name = dic[@"name"];
            student.sex = dic[@"sex"];
            student.age = dic[@"age"];
            [_studentArray addObject:student];
            [student release];
            NSLog(@"%@", student);
        }
    (2)  
            //使用jsonkit
            //获取路径
        
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];
            //获取内容
        NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
            //内容进行json解析
        NSArray *dataArray = [content objectFromJSONString];
        NSLog(@"%@", dataArray);
        for (NSDictionary *dic in dataArray) {
            Student *student = [[Student alloc] init];
            student.name = dic[@"name"];
            student.sex = dic[@"sex"];
            student.age = dic[@"age"];
            [_studentArray addObject:student];
            [student release];
            NSLog(@"%@", student);
        }

     
  • 相关阅读:
    GO語言基礎教程:數組,切片,map
    GO語言視頻教程下載
    GO語言基礎教程:流程控制
    GO語言基礎教程:數據類型,變量,常量
    GO語言基礎教程:Hello world!
    GO語言基礎教程:序章
    騰訊RTX的API開發,給RTX開個天窗
    RTX的api開發實例
    [轉]redis;mongodb;memcache三者的性能比較
    [轉載]史上最强php生成pdf文件,html转pdf文件方法
  • 原文地址:https://www.cnblogs.com/tian-sun/p/4311367.html
Copyright © 2011-2022 走看看