网络数据解析(2) JSON

来源:互联网 发布:java邮件功能 编辑:程序博客网 时间:2024/05/23 10:37
JSON (JavaScript Object Notation)是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,易于阅读和编写,同时也易于机器解析和生成.这些特性使JSON成为理想的数据交换语言.

JSON文件有两种结构: 

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

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

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

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



NSJSONSerialization工具:

#import"ThirdViewController.h"

@interfaceThirdViewController ()

@end

@implementation ThirdViewController

- (
void)viewDidLoad{
   [
superviewDidLoad];
   
self.view.backgroundColor=[UIColorcyanColor];
}

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

@end



JSONKit工具:

#import "FourthViewController.h"

#import"JSONKit.h"

@interfaceFourthViewController ()

@end

@implementation FourthViewController

- (
void)viewDidLoad{
   [
superviewDidLoad];
   
// Do any additional setup after loading theview.
}
- (
void)touchesBegan:(NSSet<</span>UITouch*>*)touches withEvent:(UIEvent*)event{
   
   
NSString *path= [[NSBundlemainBundle]pathForResource:@"JSON_stu"ofType:@"txt"];
   
NSData *data= [NSDatadataWithContentsOfFile: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"

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

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

@implementation FifthViewController

- (
void)viewDidLoad{
   [
superviewDidLoad];
   [
selfcreateTableView];
   
//获取文件路径
   
NSString *path= [[NSBundlemainBundle]pathForResource:@"movie"ofType:@"txt"];
   
//创建数据对象
   
NSData *data= [NSDatadataWithContentsOfFile:path];
   
//json数据转化成需要的格式
   
NSArray *array= [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingAllowFragmentserror:nil];   
   self.dataArray=[NSMutableArrayarrayWithArray:array];  
}
//返回cell
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{
   
   
UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"cell"];
   cell.
backgroundColor=[[UIColorwhiteColor]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=[[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:(UITableViewStylePlain)];
   
   
self.tableView.delegate=self;
   
self.tableView.dataSource=self;
   
   [
self.viewaddSubview:self.tableView];
   
   [
self.tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"cell"];
   
   
UIImageView *imageView= [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"1.jpg"]];
   
self.tableView.backgroundView=imageView;
   
}
//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
   
return self.dataArray.count;
}
//头标题
-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{  
   NSString *string= [self.dataArray[section]objectForKey:@"title"];
   
return string;
}
//行数
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
   
return [[self.dataArray[section]objectForKey:@"data"]count];
}
@end
0 0