网络数据解析(2) JSON

JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,易于阅读和编写,同时也易于机器解析和生成.这些特性使JSON成为理想的数据交换语言.

JSON文件有两种结构: 

对象: “名称/值” 对的集合 .不同的语言中,它被理解为对象,记录, 结构, 字典, 哈希表, 有键列表, 或者关联数组. 以 ” {“ 开始,以 “}” 结束, 是”名称/值” 对的集合.  名称和值中间用 “:” 隔开, 多个 “名称/值” 对之间用 “,” 隔开 .

数组: 值的有序列表. 在大部分语言中, 它被理解为数组 . 以 “[“ 开始, 以 ”]” 结束, 中间是数据. 数据已 “,” 分隔.
 
JSON中的数据类型 : 字符串, 数值 ,BOOL, 对象,数组

 网络数据解析(2) <wbr>JSON

功能: 
  1. 数据交换
  2. 内容管理
  3. 配置文件



NSJSONSerialization工具:

#import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (
void )viewDidLoad {
    [
super viewDidLoad ];
   
self . view . backgroundColor = [ UIColor cyanColor ];
}

- (
void )touchesBegan:( NSSet <</span>UITouch *> *)touches withEvent:(UIEvent *)event{
   // 获取文件路径
    NSString *path = [[ NSBundle mainBundle ] pathForResource : @"JSON_stu" ofType : @"txt" ];
    // 创建数据对象
    NSData *data = [ NSData dataWithContentsOfFile :path];
   
//    NSJSONReadingMutableContainers  返回一个数组或者字典
//    NSJSONReadingMutableLeaves  返回一个字符串
//    NSJSONReadingAllowFragments 返回一个任意类型的对象
   
       // json 数据转化成需要的格式
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments  error:nil];
    NSLog ( @"array --> %@" ,array);
   
   
for ( NSDictionary *dict in array) {
       
NSLog ( @"dict --> %@" ,[dict objectForKey : @"content" ]);
    }
   
}

@end



JSONKit工具:

#import "FourthViewController.h"

#import "JSONKit.h"

@interface FourthViewController ()

@end

@implementation FourthViewController

- (
void )viewDidLoad {
    [
super viewDidLoad ];
   
// Do any additional setup after loading the view.
}
- (
void )touchesBegan:( NSSet <</span>UITouch *> *)touches withEvent:(UIEvent *)event{
   
   
NSString *path = [[NSBundle mainBundle]pathForResource:@"JSON_stu" ofType:@"txt"];
   
NSData *data = [NSData dataWithContentsOfFile:path];
   
//json数据转化成需要的格式
   
NSArray *array = [data objectFromJSONData];
   
   
NSLog(@"array ---> %@",array);
   
}


@end


JSON优缺点:

优点: 
  1. 数据格式比较简单,易于读写,格式都是压缩的,占用带宽小.
  2. 易于解析这种语言
  3. 支持多种语言,包括ActionScript ,C, C#, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby等语言服务器端语言,便于服务器端的解析
  4. 因为JSON格式能够直接为服务器端代码使用, 大大简化了服务器端和客户端的代码开发量,但是完成的任务不变, 且易于维护.



配合tableView 
#import "FifthViewController.h"

@interface FifthViewController ()<</span>UITableViewDelegate,UITableViewDataSource>
//数据源数组
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) UITableView *tableView;
@end

@implementation FifthViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
    [
self createTableView];
   
//获取文件路径
   
NSString *path = [[NSBundle mainBundle]pathForResource:@"movie" ofType:@"txt"];
   
//创建数据对象
   
NSData *data = [NSData dataWithContentsOfFile:path];
   
//json数据转化成需要的格式
   
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];   
    self.dataArray = [NSMutableArray arrayWithArray:array];    
}
//返回cell
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
   
   
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier : @"cell" ];
    cell.
backgroundColor = [[ UIColor whiteColor ] colorWithAlphaComponent :0];
    cell.
selectionStyle = UITableViewCellSelectionStyleNone ;
   
       
// 取出来的是数组包含的字典
       
NSDictionary *dic1 = self . dataArray [indexPath. section ];
       
// 通过 key :data 取出一个数组
       
NSArray *arr = [dic1 objectForKey : @"data" ];
   
NSDictionary *dic2 = [arr objectAtIndex :indexPath. row ];
    cell.
textLabel . text = [dic2 objectForKey : @"title" ];
   
   
return cell;
   
}
//创建tableView
- ( void )createTableView{
   
   
self . tableView = [[ UITableView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds style :( UITableViewStylePlain )];
   
   
self . tableView . delegate = self ;
   
self . tableView . dataSource = self ;
   
    [
self . view addSubview : self . tableView ];
   
    [
self . tableView registerClass :[ UITableViewCell class ] forCellReuseIdentifier : @"cell" ];
   
   
UIImageView *imageView = [[ UIImageView alloc ] initWithImage :[ UIImage imageNamed : @"1.jpg" ]];
   
self . tableView . backgroundView = imageView;
   
}
//分区数
- ( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView{
   
return self . dataArray . count ;
}
//头标题
- ( NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section{    
    NSString *string = [ self . dataArray [section] objectForKey : @"title" ];
   
return string;
}
//行数
- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section{
   
return [[ self . dataArray [section] objectForKey : @"data" ] count ];
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值