码迷,mamicode.com
首页 > Web开发 > 详细

JSON

时间:2015-09-18 18:12:34      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

//json解析

//系统 效率最高

- (IBAction)handleXT:(UIBarButtonItem *)sender {

    [self.dataArray removeAllObjects];

    // 1.JSON格式的对象解析为OC对象

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];

    //转化为 NSData 对象

    NSData *data = [NSData dataWithContentsOfFile:path];

    //解析

    self.dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    

    //2.oc 对象转化为Json 数据

//    NSArray *array1 = @[@"aa", @"bb", @"hh", @"dd", @"tt" ];

//    NSDictionary *dic = @{@"key":array1};

//    

//    NSData *data2 =[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];

    

    [self.tableView reloadData];

  

}

- (IBAction)handleThird:(UIBarButtonItem *)sender {

    

    [self.dataArray removeAllObjects];

    

    //JSON数据转化为 oc 对象

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

    NSData *data = [NSData dataWithContentsOfFile:path];

    self.dataArray = [data objectFromJSONData];

//    NSLog(@"%@", self.dataArray);

    

    //2.转化为NSString 对象

//    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

//    self.dataArray = [str objectFromJSONString];

//    NSLog(@"++%@", self.dataArray);

    

    //oc 对象转化为Json 数据

//    NSArray *array1 = @[@"aa", @"bb", @"hh", @"dd", @"tt" ];

//    NSData *dataArr = [array1 JSONData];

//    NSDictionary *dic = @{@"key":array1};

//    NSData *data3 = [dic JSONData];

    

    [self.tableView reloadData];

}

JSON

标签:

原文地址:http://www.cnblogs.com/tapple/p/4819958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!