網(wǎng)絡(luò)請(qǐng)求之JSON和XML

1. JSON

  • 什么是JSON

    • JSON是一種輕量級(jí)的數(shù)據(jù)格式,一般用于數(shù)據(jù)交互
    • 服務(wù)器返回給客戶端的數(shù)據(jù),一般都是JSON格式或者XML格式(文件下載除外)
  • JSON的格式很像OC中的字典和數(shù)組

{"name" : "jack", "age" : 10}
{"names" : ["jack", "rose", "jim"]}
  • 標(biāo)準(zhǔn)JSON格式的注意點(diǎn):key必須用雙引號(hào)

  • 要想從JSON中挖掘出具體數(shù)據(jù),得對(duì)JSON進(jìn)行解析

    • JSON 轉(zhuǎn)換為 OC數(shù)據(jù)類型

1.1JSON – OC 轉(zhuǎn)換對(duì)照表

JSON OC
大括號(hào){} NSDictionary
中括號(hào)[] NSArray
雙引號(hào)"" NSString
數(shù)字10、10.8 NSNumber

1.2 JSON解析方案

  • 在iOS中,JSON的常見解析方案有4種
    • 第三方框架:JSONKit、SBJson、TouchJSON(性能從左到右,越差)

    • 蘋果原生(自帶):NSJSONSerialization(性能最好)

      • NSJSONSerialization的常見方法
        • JSON數(shù)據(jù) --> ? OC對(duì)象 (反序列化)
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
  • OC對(duì)象 -->? JSON數(shù)據(jù) (序列化)
+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;

1.3 客戶端解析來(lái)自服務(wù)器的JSON過(guò)程

客戶端解析來(lái)自服務(wù)器的JSON過(guò)程.png

代碼如下:

#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)建請(qǐng)求對(duì)象
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    // 3. 發(fā)送網(wǎng)絡(luò)請(qǐng)求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
        
        // data --> 本質(zhì)上是一個(gè)json字符串
        // 4. 解析JSON
        //        NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        
        // JSON --> OC對(duì)象 反序列化
        /*
         第一參數(shù): JSON的二進(jìn)制數(shù)據(jù)
         第二參數(shù):
         第三參數(shù): 錯(cuò)誤信息
         */
        /*
         NSJSONReadingMutableContainers = (1UL << 0),  可變字典和數(shù)組
         NSJSONReadingMutableLeaves = (1UL << 1),       內(nèi)部所有的字符串都是可變的,ios7之后有問(wèn)題,一般不用
         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];  // 該方法獲得的是一個(gè)單例,表示為空,可以用在字典或者是數(shù)組中
}

-(void)OCToJson
{
    NSDictionary * dicM = @{
                            @"name":@"CWJ",
                            @"age":@25
                            };
    
    NSArray * arrM = @[@"123",@"456"];
    
    
    // 注意: 并不是所有的OC對(duì)象都能轉(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不能是無(wú)窮大
     */
    NSString * strM = @"hahahah"; // 不能轉(zhuǎn)換
    BOOL isValid =  [NSJSONSerialization isValidJSONObject:strM];
    if (!isValid) {
        NSLog(@"是否轉(zhuǎn)換:%zd",isValid);
        return;
    }
    
    
    // OC --> JSON 序列化
    /*
     第一參數(shù): 要轉(zhuǎn)換的OC對(duì)象
     第二參數(shù): 選項(xiàng) NSJSONWritingPrettyPrinted  排版 美觀
     第三參數(shù):錯(cuò)誤信息
     */
    /*
     沒(méi)排版的模式: {"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-掌握J(rèn)SON解析/apps.plist"];
    NSLog(@"%@",arrM);
    
//    [arrM writeToFile:@"/Users/WJim/Desktop/GITEE/DuoXianChengDeXueXi/Day15/01-掌握J(rèn)SON解析/123.json" atomically:YES];
    
    
    // OC --> JSON
    NSData * data  = [NSJSONSerialization dataWithJSONObject:arrM options:NSJSONWritingPrettyPrinted error:0];
    
    [data writeToFile:@"/Users/WJim/Desktop/GITEE/DuoXianChengDeXueXi/Day15/01-掌握J(rèn)SON解析/123.json" atomically:YES];
    
}

@end

2. XML

  • 什么是XML

    • 全稱是Extensible Markup Language,譯作“可擴(kuò)展標(biāo)記語(yǔ)言”
    • 跟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語(yǔ)法

  • 一個(gè)常見的XML文檔一般由以下部分組成
    • 文檔聲明
    • 元素(Element)
    • 屬性(Attribute)

2.1.1 XML語(yǔ)法 – 文檔聲明

  • 在XML文檔的最前面,必須編寫一個(gè)文檔聲明,用來(lái)聲明XML文檔的類型
    • 最簡(jiǎn)單的聲明
<?xml version="1.0" ?>
  • 用encoding屬性說(shuō)明文檔的字符編碼
<?xml version="1.0" encoding="UTF-8" ?>

2.1.2 XML語(yǔ)法 – 元素(Element)

  • 一個(gè)元素包括了開始標(biāo)簽和結(jié)束標(biāo)簽

    • 擁有內(nèi)容的元素:<video>小黃人</video>
    • 沒(méi)有內(nèi)容的元素:<video></video>
    • 沒(méi)有內(nèi)容的元素簡(jiǎn)寫:<video/>
  • 一個(gè)元素可以嵌套若干個(gè)子元素(不能出現(xiàn)交叉嵌套)

<videos>
    <video>
        <name>小黃人 第01部</name>
          <length>30</length>
    </video>
</videos>
  • 規(guī)范的XML文檔最多只有1個(gè)根元素,其他元素都是根元素的子孫元素

2.1.3 XML語(yǔ)法 –元素的注意

  • XML中的所有空格和換行,都會(huì)當(dāng)做具體內(nèi)容處理
  • 下面兩個(gè)元素的內(nèi)容是不一樣的
    • 第1個(gè)
<video>小黃人</video>
  • 第2個(gè)
<video>
    小黃人
</video>

2.1.4 XML語(yǔ)法 – 屬性(Attribute)

  • 一個(gè)元素可以擁有多個(gè)屬性
<video name="小黃人 第01部" length="30" />
  • video元素?fù)碛衝ame和length兩個(gè)屬性

  • 屬性值必須用 雙引號(hào)"" 或者 單引號(hào)'' 括住

  • 實(shí)際上,屬性表示的信息也可以用子元素來(lái)表示,比如

<video>
    <name>小黃人 第01部</name>
        <length>30</length>
</video>

2.1.5 XML解析

  • 要想從XML中提取有用的信息,必須得學(xué)會(huì)解析XML
    • 提取name元素里面的內(nèi)容
<name>小黃人 第01部</name>
  • 提取video元素中name和length屬性的值
<video name="小黃人 第01部" length="30" />
  • XML的解析方式有2種
    • DOM:一次性將整個(gè)XML文檔加載進(jìn)內(nèi)存,比較適合解析小文件
    • SAX:從根元素開始,按順序一個(gè)元素一個(gè)元素往下解析,比較適合解析大文件

2.1.6 iOS中的XML解析

  • 在iOS中,解析XML的手段有很多

    • 蘋果原生

      • NSXMLParser:SAX方式解析,使用簡(jiǎn)單
    • 第三方框架

      • libxml2:純C語(yǔ)言,默認(rèn)包含在iOS SDK中,同時(shí)支持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)聽解析過(guò)程
parser.delegate = self;
// 開始解析,解析過(guò)程阻塞式的
[parser parse];

  • NSXMLParser采取的是SAX方式解析,特點(diǎn)是事件驅(qū)動(dòng),下面情況都會(huì)通知代理
    • 當(dāng)掃描到文檔(Document)的開始與結(jié)束
    • 當(dāng)掃描到元素(Element)的開始與結(jié)束

2.1.8 NSXMLParserDelegate

  • 當(dāng)掃描到文檔的開始時(shí)調(diào)用(開始解析)
- (void)parserDidStartDocument:(NSXMLParser *)parser
  • 當(dāng)掃描到文檔的結(jié)束時(shí)調(diào)用(解析完畢)
- (void)parserDidEndDocument:(NSXMLParser *)parser
  • 當(dāng)掃描到元素的開始時(shí)調(diào)用(attributeDict存放著元素的屬性)
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
  • 當(dāng)掃描到元素的結(jié)束時(shí)調(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;
        // 沒(méi)有分割線
        //        _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è)置多少個(gè)分組
-(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對(duì)應(yīng)的數(shù)據(jù)
    //    NSDictionary * dicM = self.videoArray[indexPath.row];
    
    self.videoModel = self.videoArray[indexPath.row];
    
    // 2.2 設(shè)置標(biāo)題
    cell.textLabel.text = self.videoModel.name;
    
    // 2.3 設(shè)置子標(biāo)題
    cell.detailTextLabel.text = [NSString stringWithFormat:@"播放時(shí)間:%@",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 - 點(diǎn)擊對(duì)應(yīng)的 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ù)請(qǐng)求
-(void)NetWorkURL
{
    
    
    // 1. 確定URL
    NSURL * url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=XML"];
    // 2. 創(chuàng)建發(fā)送對(duì)象
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    // 3. 發(fā)送請(qǐng)求
    [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文檔的時(shí)候
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    NSLog(@"%s",__func__);
}

// 2. 開始解析某個(gè)元素
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
    NSLog(@"開始解析: %@-----%@",elementName,attributeDict);
    
    // 過(guò)濾根元素
    if ([elementName isEqualToString:@"videos"]) {
        return;
    }
    
    // 字典轉(zhuǎn)模型
    [self.videoArray addObject:[VideoModel mj_objectWithKeyValues:attributeDict]];
}

// 3. 某個(gè)元素解析完畢
-(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • JSON JSON和XML都是需要解析的 JSON是一種輕量級(jí)的數(shù)據(jù)格式,一般用于數(shù)據(jù)交互服務(wù)器返回給客戶端的數(shù)據(jù)...
    JonesCxy閱讀 2,005評(píng)論 2 10
  • JSON 和 XML 一、JSON 1.什么是JSON JSON是一種輕量級(jí)的數(shù)據(jù)格式,一般用于數(shù)據(jù)交互服務(wù)器返回...
    妳是我的天使閱讀 413評(píng)論 0 3
  • 簡(jiǎn)介 JSON是一種輕量級(jí)的數(shù)據(jù)格式,一般用于數(shù)據(jù)交互 服務(wù)器返回給客戶端的數(shù)據(jù),一般都是JSON格式或者XML格...
    SanMao_SFW閱讀 571評(píng)論 0 1
  • 本文主要介紹了XML和JSON數(shù)據(jù)解析的基本知識(shí),并展示了NSXMLParser方法、GDataXML第三方庫(kù)以及...
    Rim99閱讀 2,564評(píng)論 1 21
  • JSON 什么是JSON JSON是一種輕量級(jí)的數(shù)據(jù)格式,一般用于數(shù)據(jù)交互. 服務(wù)器返回給客戶端的數(shù)據(jù),一般都是J...
    coma閱讀 345評(píng)論 0 1

友情鏈接更多精彩內(nèi)容