网络数据解析(2) JSON
来源:互联网 发布:java邮件功能 编辑:程序博客网 时间:2024/05/23 10:37
JSON (JavaScript Object Notation)是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,易于阅读和编写,同时也易于机器解析和生成.这些特性使JSON成为理想的数据交换语言.
JSON文件有两种结构:
对象: “名称/值” 对的集合 .不同的语言中,它被理解为对象,记录,结构, 字典, 哈希表, 有键列表, 或者关联数组. 以 ” {“ 开始,以 “}” 结束, 是”名称/值” 对的集合. 名称和值中间用 “:” 隔开, 多个 “名称/值” 对之间用 “,” 隔开.
数组: 值的有序列表. 在大部分语言中, 它被理解为数组 . 以 “[“开始, 以 ”]” 结束, 中间是数据. 数据已 “,” 分隔.
JSON中的数据类型 : 字符串, 数值 ,BOOL,对象,数组
功能:
- 数据交换
- 内容管理
- 配置文件
NSJSONSerialization工具:
#import"ThirdViewController.h"
@interfaceThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad{
}
- (void)touchesBegan:(NSSet<</span>UITouch*>*)touches withEvent:(UIEvent*)event{
//
//
}
@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);
}
#import"JSONKit.h"
@interfaceFourthViewController ()
@end
@implementation FourthViewController
- (void)viewDidLoad{
}
- (void)touchesBegan:(NSSet<</span>UITouch*>*)touches withEvent:(UIEvent*)event{
}
@end
JSON优缺点:
优点:
- 数据格式比较简单,易于读写,格式都是压缩的,占用带宽小.
- 易于解析这种语言
- 支持多种语言,包括ActionScript ,C, C#, ColdFusion, Java, JavaScript,Perl, PHP, Python, Ruby等语言服务器端语言,便于服务器端的解析
- 因为JSON格式能够直接为服务器端代码使用, 大大简化了服务器端和客户端的代码开发量,但是完成的任务不变,且易于维护.
配合tableView
#import "FifthViewController.h"
@interfaceFifthViewController ()<</span>UITableViewDelegate,UITableViewDataSource>
//数据源数组
@property(nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation FifthViewController
- (void)viewDidLoad{
}
//返回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{
}
//行数
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
return [[self.dataArray[section]objectForKey:@"data"]count];
}
@end
0 0
- 网络数据解析(2) JSON
- JSON 数据解析
- UI-json解析网络数据
- 网络编程--JSON数据解析
- Json解析网络请求数据
- 网络数据解析(1) XML
- 网络数据传递之 Json数据解析
- iOS 网络数据请求和Json 解析
- android解析网络json数据(1)
- 【iOS开发-网络】解析json数据
- 网络访问之json数据解析
- 访问网络数据+json解析代码片段
- ios网络数据下载和JSON解析
- Json解析网络数据+listview展示跳转
- Android 网络获取数据之Json解析
- httpURLConnection获得网络数据解析json串
- 网络编程-json数据的解析
- AsyncTask 异步任务 + json 解析 网络数据
- sqlite3使用简介
- SQLite常用的函数
- iOS开发网络篇—数据缓存
- NSPredicate谓词
- 网络数据解析(1) XML
- 网络数据解析(2) JSON
- 网络请求(NSURLConnection)
- android pdf 阅读器开发, pdf demo, pdf第三方控件
- 网络请求(NSURLSession)
- 多线程
- 网络安全
- 调用系统相册
- 搜索栏 UISearchController
- Swift (一)