1. JSON
-
什么是JSON
- JSON是一種輕量級的數(shù)據(jù)格式各薇,一般用于數(shù)據(jù)交互
- 服務(wù)器返回給客戶端的數(shù)據(jù)筷笨,一般都是JSON格式或者XML格式(文件下載除外)
JSON的格式很像OC中的字典和數(shù)組
{"name" : "jack", "age" : 10}
{"names" : ["jack", "rose", "jim"]}
標準JSON格式的注意點:key必須用雙引號
-
要想從JSON中挖掘出具體數(shù)據(jù)誊酌,得對JSON進行解析
- JSON 轉(zhuǎn)換為 OC數(shù)據(jù)類型
1.1JSON – OC 轉(zhuǎn)換對照表
JSON | OC |
---|---|
大括號{} | NSDictionary |
中括號[] | NSArray |
雙引號"" | NSString |
數(shù)字10、10.8 | NSNumber |
1.2 JSON解析方案
- 在iOS中屯蹦,JSON的常見解析方案有4種
第三方框架:JSONKit、SBJson雄驹、TouchJSON(性能從左到右,越差)
-
蘋果原生(自帶):NSJSONSerialization(性能最好)
- NSJSONSerialization的常見方法
- JSON數(shù)據(jù) --> ? OC對象 (反序列化)
- NSJSONSerialization的常見方法
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
- OC對象 -->? JSON數(shù)據(jù) (序列化)
+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;
1.3 客戶端解析來自服務(wù)器的JSON過程
代碼如下:
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIButton * btn;
@end
@implementation ViewController
-(UIButton *)btn
{
if (!_btn) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.frame = CGRectMake(SCREENWIDTH / 2 - 75, SCREENHEIGHT / 2 - 15, 150, 30);
[_btn setTitle:@"登錄" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
-(void)clickBtn:(UIButton *)btn
{
// [self jsonToOC];
// [self JSONWithOC];
// [self OCToJson];
[self test];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.btn];
}
-(void)jsonToOC
{
// 1. 確定URL
NSURL * url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=132&pwd=123&type=JSON"];
// 2. 創(chuàng)建請求對象
NSURLRequest * request = [NSURLRequest requestWithURL:url];
// 3. 發(fā)送網(wǎng)絡(luò)請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
// data --> 本質(zhì)上是一個json字符串
// 4. 解析JSON
// NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
// JSON --> OC對象 反序列化
/*
第一參數(shù): JSON的二進制數(shù)據(jù)
第二參數(shù):
第三參數(shù): 錯誤信息
*/
/*
NSJSONReadingMutableContainers = (1UL << 0), 可變字典和數(shù)組
NSJSONReadingMutableLeaves = (1UL << 1), 內(nèi)部所有的字符串都是可變的淹辞,ios7之后有問題荠医,一般不用
NSJSONReadingAllowFragments = (1UL << 2) 既不是字典,也不是數(shù)組,則必須使用該枚舉值
*/
// NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// NSLog( @"解析到的數(shù)據(jù): %@",dic[@"error"]);
NSString * str = @"\"asdasdasdasd\"";
id obj = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
NSLog( @"解析到的數(shù)據(jù): %@-------%@",[obj class],obj);
}];
}
-(void)JSONWithOC
{
// NSString * strM = @"\{\"error\":\"用戶名不存在\"}";
// NSString * strM = @"[\"error\",\"用戶名不存在\"]";
// NSString * strM = @"\"dasdqwdxc\"";
// NSString * strM = @"false";
// NSString * strM = @"true";
NSString * strM = @"null";
id obj = [NSJSONSerialization JSONObjectWithData:[strM dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:0];
NSLog(@"%@------%@",[obj class],obj);
/*
JSON OC
{} @{} 字典
[] @[] 數(shù)組
"" @""
flase NSNumber 0
true NSNumber 1
null NSNull 為空
*/
// nil
[NSNull null]; // 該方法獲得的是一個單例彬向,表示為空兼贡,可以用在字典或者是數(shù)組中
}
-(void)OCToJson
{
NSDictionary * dicM = @{
@"name":@"CWJ",
@"age":@25
};
NSArray * arrM = @[@"123",@"456"];
// 注意: 并不是所有的OC對象都能轉(zhuǎn)換為JSON
/*
- Top level object is an NSArray or NSDictionary 最外層必須是 NSArray or NSDictionary
- All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull 所有的元素必須是 NSString, NSNumber, NSArray, NSDictionary, or NSNull
- All dictionary keys are NSStrings 字典里所有的key都必須是 NSStrings類型的
- NSNumbers are not NaN or infinity NSNumbers不能是無窮大
*/
NSString * strM = @"hahahah"; // 不能轉(zhuǎn)換
BOOL isValid = [NSJSONSerialization isValidJSONObject:strM];
if (!isValid) {
NSLog(@"是否轉(zhuǎn)換:%zd",isValid);
return;
}
// OC --> JSON 序列化
/*
第一參數(shù): 要轉(zhuǎn)換的OC對象
第二參數(shù): 選項 NSJSONWritingPrettyPrinted 排版 美觀
第三參數(shù):錯誤信息
*/
/*
沒排版的模式: {"name":"CWJ","age":25}
排版的模式:
{
"name" : "CWJ",
"age" : 25
}
*/
NSData * data = [NSJSONSerialization dataWithJSONObject:arrM options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
-(void)test
{
NSArray * arrM = [NSArray arrayWithContentsOfFile:@"/Users/WJim/Desktop/GITEE/DuoXianChengDeXueXi/Day15/01-掌握JSON解析/apps.plist"];
NSLog(@"%@",arrM);
// [arrM writeToFile:@"/Users/WJim/Desktop/GITEE/DuoXianChengDeXueXi/Day15/01-掌握JSON解析/123.json" atomically:YES];
// OC --> JSON
NSData * data = [NSJSONSerialization dataWithJSONObject:arrM options:NSJSONWritingPrettyPrinted error:0];
[data writeToFile:@"/Users/WJim/Desktop/GITEE/DuoXianChengDeXueXi/Day15/01-掌握JSON解析/123.json" atomically:YES];
}
@end
2. XML
-
什么是XML
- 全稱是Extensible Markup Language,譯作“可擴展標記語言”
- 跟JSON一樣娃胆,也是常用的一種用于交互的數(shù)據(jù)格式
- 一般也叫XML文檔(XML Document)
XML舉例
<videos>
<video name="小黃人 第01部" length="30" />
<video name="小黃人 第02部" length="19" />
<video name="小黃人 第03部" length="33" />
</videos>
2.1 XML語法
- 一個常見的XML文檔一般由以下部分組成
- 文檔聲明
- 元素(Element)
- 屬性(Attribute)
2.1.1 XML語法 – 文檔聲明
- 在XML文檔的最前面遍希,必須編寫一個文檔聲明,用來聲明XML文檔的類型
- 最簡單的聲明
<?xml version="1.0" ?>
- 用encoding屬性說明文檔的字符編碼
<?xml version="1.0" encoding="UTF-8" ?>
2.1.2 XML語法 – 元素(Element)
-
一個元素包括了開始標簽和結(jié)束標簽
- 擁有內(nèi)容的元素:<video>小黃人</video>
- 沒有內(nèi)容的元素:<video></video>
- 沒有內(nèi)容的元素簡寫:<video/>
一個元素可以嵌套若干個子元素(不能出現(xiàn)交叉嵌套)
<videos>
<video>
<name>小黃人 第01部</name>
<length>30</length>
</video>
</videos>
- 規(guī)范的XML文檔最多只有1個根元素里烦,其他元素都是根元素的子孫元素
2.1.3 XML語法 –元素的注意
- XML中的所有空格和換行凿蒜,都會當做具體內(nèi)容處理
- 下面兩個元素的內(nèi)容是不一樣的
- 第1個
<video>小黃人</video>
- 第2個
<video>
小黃人
</video>
2.1.4 XML語法 – 屬性(Attribute)
- 一個元素可以擁有多個屬性
<video name="小黃人 第01部" length="30" />
video元素擁有name和length兩個屬性
屬性值必須用 雙引號"" 或者 單引號'' 括住
實際上,屬性表示的信息也可以用子元素來表示胁黑,比如
<video>
<name>小黃人 第01部</name>
<length>30</length>
</video>
2.1.5 XML解析
- 要想從XML中提取有用的信息废封,必須得學會解析XML
- 提取name元素里面的內(nèi)容
<name>小黃人 第01部</name>
- 提取video元素中name和length屬性的值
<video name="小黃人 第01部" length="30" />
- XML的解析方式有2種
- DOM:一次性將整個XML文檔加載進內(nèi)存,比較適合解析小文件
- SAX:從根元素開始丧蘸,按順序一個元素一個元素往下解析漂洋,比較適合解析大文件
2.1.6 iOS中的XML解析
-
在iOS中,解析XML的手段有很多
-
蘋果原生
- NSXMLParser:SAX方式解析力喷,使用簡單
-
第三方框架
- libxml2:純C語言刽漂,默認包含在iOS SDK中,同時支持DOM和SAX方式解析
- GDataXML:DOM方式解析弟孟,由Google開發(fā)贝咙,基于libxml2
-
-
XML解析方式的選擇建議
- 大文件:NSXMLParser、libxml2
- 小文件:GDataXML拂募、NSXMLParser庭猩、libxml2
2.1.7 NSXMLParser
- 使用步驟
// 傳入XML數(shù)據(jù),創(chuàng)建解析器
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
// 設(shè)置代理陈症,監(jiān)聽解析過程
parser.delegate = self;
// 開始解析,解析過程阻塞式的
[parser parse];
- NSXMLParser采取的是SAX方式解析眯娱,特點是事件驅(qū)動,下面情況都會通知代理
- 當掃描到文檔(Document)的開始與結(jié)束
- 當掃描到元素(Element)的開始與結(jié)束
2.1.8 NSXMLParserDelegate
- 當掃描到文檔的開始時調(diào)用(開始解析)
- (void)parserDidStartDocument:(NSXMLParser *)parser
- 當掃描到文檔的結(jié)束時調(diào)用(解析完畢)
- (void)parserDidEndDocument:(NSXMLParser *)parser
- 當掃描到元素的開始時調(diào)用(attributeDict存放著元素的屬性)
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
- 當掃描到元素的結(jié)束時調(diào)用
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
- 具體代碼如下:
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "VideoModel.h"
#import "UIImageView+WebCache.h"
#import "MJExtension.h"
//#define baseURLStr @"http://120.25.226.186:32812";
static NSString * baseURLStr = @"http://120.25.226.186:32812";
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,NSXMLParserDelegate>
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * videoArray;
@property (nonatomic,strong) VideoModel * videoModel;
@end
@implementation ViewController
#pragma mark - lazyLoading
-(UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.showsVerticalScrollIndicator = NO;
// 沒有分割線
// _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return _tableView;
}
#pragma mark - 可變數(shù)組懶加載
-(NSMutableArray *)videoArray
{
if (!_videoArray) {
_videoArray = [NSMutableArray array];
}
return _videoArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self NetWorkURL];
[self.view addSubview:self.tableView];
}
#pragma mark - 設(shè)置多少個分組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark - 設(shè)置分組里需要多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.videoArray.count;
}
#pragma mark - 設(shè)置分組每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 150;
}
#pragma mark - 填充tableview 各組各行的內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * Identifier = @"CellID";
UITableViewCell * cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Identifier];
}
// 2.1 拿到該cell對應的數(shù)據(jù)
// NSDictionary * dicM = self.videoArray[indexPath.row];
self.videoModel = self.videoArray[indexPath.row];
// 2.2 設(shè)置標題
cell.textLabel.text = self.videoModel.name;
// 2.3 設(shè)置子標題
cell.detailTextLabel.text = [NSString stringWithFormat:@"播放時間:%@",self.videoModel.length];
// NSString * baseURLStr = @"http://120.25.226.186:32812";
NSString * urlStr = [baseURLStr stringByAppendingPathComponent:self.videoModel.image];
// 2.4 設(shè)置圖片
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"home_forum_holder"]];
NSLog(@"-----%@",self.videoModel.ID);
//去掉底部多余的表格線
[tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
return cell;
}
#pragma mark - 點擊對應的 tableview 的效果
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// 1. 拿到數(shù)據(jù)
// NSDictionary * dic = self.videoArray[indexPath.row];
self.videoModel = self.videoArray[indexPath.row];
// 2. 拼接數(shù)據(jù)
NSString * urlStr = [baseURLStr stringByAppendingPathComponent:self.videoModel.url];
// 3. 創(chuàng)建播放器
MPMoviePlayerViewController * videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlStr]];
// 4. 彈出控制器
[self presentViewController:videoPlayer animated:YES completion:nil];
}
#pragma mark - 網(wǎng)絡(luò)數(shù)據(jù)請求
-(void)NetWorkURL
{
// 1. 確定URL
NSURL * url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=XML"];
// 2. 創(chuàng)建發(fā)送對象
NSURLRequest * request = [NSURLRequest requestWithURL:url];
// 3. 發(fā)送請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError) {
return ;
}
// 4. 解析數(shù)據(jù)
// 4.1 創(chuàng)建XML解析器:SAX
NSXMLParser * parser = [[NSXMLParser alloc] initWithData:data];
// 4.2 設(shè)置代理
parser.delegate = self;
// 4.3 開始解析,阻塞
[parser parse];
// 5. 更新UI
[self.tableView reloadData];
}];
}
#pragma mark - NSXMLParserDelegate
// 1. 開始解析XML文檔的時候
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(@"%s",__func__);
}
// 2. 開始解析某個元素
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
NSLog(@"開始解析: %@-----%@",elementName,attributeDict);
// 過濾根元素
if ([elementName isEqualToString:@"videos"]) {
return;
}
// 字典轉(zhuǎn)模型
[self.videoArray addObject:[VideoModel mj_objectWithKeyValues:attributeDict]];
}
// 3. 某個元素解析完畢
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"結(jié)束解析: %@",elementName);
}
// 4. 結(jié)束解析
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"%s",__func__);
}
@end