iOS開發(fā)使用JSON解析網(wǎng)絡數(shù)據(jù)
前言:對服務器請求之后,返回給客戶端的數(shù)據(jù),一般都是JSON格式或者XML格式(文件下載除外)
本篇隨便先講解JSON解析。
正文:
關于JSON:
JSON是一種輕量級的數(shù)據(jù)格式,一般用于數(shù)據(jù)交互JSON的格式很像Objective-C中的字典和數(shù)組:{"name":"jack","age":10}
補充:
標準的JSON格式的注意點:key必須用雙引號。(但是在Java中是單引號)
JSON-OC的轉換對照表
其中:null--返回OC里的NSNull類型
使用:
在JSON解析方案有很多種,但是(蘋果原生的)NSJSONSerialization性能最好
反序列化(JSON --> OC對象),下面示例解析成字典對象
序列化(OC對象 --> JSON),注意字典的值不能傳nil,但是可以傳[NSNull null]
并不是所有的類型都是可以轉為JSON的
以下是蘋果官方規(guī)定:
我們再來看個實例:
#import "MainViewController.h" #import "Video.h" #define kBaseURL @"http://192.168.3.252/~apple" @interface MainViewController () @property (strong, nonatomic) NSArray *dataList; @property (weak, nonatomic) UITableView *tableView; @end @implementation MainViewController class="p1"> "412158" snippet_file_name="blog_20140630_1_3481337" name="code" class="objc"> #pragma mark 實例化視圖 - (void)loadView { self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame]; //1 tableview CGRect frame = self.view.bounds; UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 44) style:UITableViewStylePlain]; //1)數(shù)據(jù)源 [tableView setDataSource:self]; //2)代理 [tableView setDelegate:self]; //)設置表格高度 [tableView setRowHeight:80]; [self.view addSubview:tableView]; self.tableView = tableView; //toolBar UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, tableView.bounds.size.height, 320, 44)]; [self.view addSubview:toolBar]; //添加toolbar按鈕 UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"load json" style:UIBarButtonItemStyleDone target:self action:@selector(loadJson)]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"load xml" style:UIBarButtonItemStyleDone target:self action:@selector(loadXML)]; UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [toolBar setItems:@[item3, item1, item3, item2, item3]]; } #pragma mark -uitableview數(shù)據(jù)源方法 對于uitableview下面這兩個方法是必須實現(xiàn)的。 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataList.count; } //每填充一行都調(diào)用一次這個方法。知道界面上的所有行都填充完畢。, - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //使用可充用標示符查詢可重用單元格 static NSString *ID = @"MyCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } //設置單元格內(nèi)容 Video *v = self.dataList[indexPath.row]; cell.textLabel.text = v.name; cell.detailTextLabel.text = v.teacher; //加載圖片 //1)同步加載網(wǎng)絡圖片,同步方法以為這這些指令在完成之前,后續(xù)指令都無法執(zhí)行。 //注意:在開發(fā)網(wǎng)絡應用時,不要使用同步方法加載圖片,否則會嚴重影響用戶體驗 // NSString *imagePath = [NSString stringWithFormat:@"%@%@", kBaseURL, v.imageURL]; // NSURL *imageUrl = [NSURL URLWithString:imagePath]; // NSData *imageData = [NSData dataWithContentsOfURL:imageUrl]; // UIImage *image = [UIImage imageWithData:imageData]; // // //2)異步加載網(wǎng)絡圖片 // //網(wǎng)絡連接本身就有異步命令 sendAsync // [cell.imageView setImage:image]; //如果緩存圖像不存在 if (v.cacheImage == nil) { //使用默認圖像占位,即能夠保證有圖像,又能夠保證有地方。 UIImage *image = [UIImage imageNamed:@"user_default.png"]; [cell.imageView setImage:image]; //使用默認圖像占位 //開啟異步連接,加載圖像,因為加載完成之后,需要刷新對應的表格航 [self loadImageAsyncWithIndexPath:indexPath]; }else { [cell.imageView setImage:v.cacheImage]; } //[self loadImageAsyncWithUrl:imageUrl imageView:cell.imageView]; return cell; } #pragma mark 異步加載網(wǎng)絡圖片 //由于uitableview是可重用的,為了避免用戶快速頻繁刷新表格,造成數(shù)據(jù)沖突,不能直接將uiimageview傳入異步方法 //正確的解決方法是:將表格行的indexpath傳入異步方法,加載完成圖像以后,直接刷新指定的行。 - (void)loadImageAsyncWithIndexPath:(NSIndexPath *)indexPath { Video *v = self.dataList[indexPath.row]; //取出當前要填充的行 NSString *imagePath = [NSString stringWithFormat:@"%@%@", kBaseURL, v.imageURL]; NSURL *imageUrl = [NSURL URLWithString:imagePath]; //NSLog(@"%@ %@", url, imageView); //1 request NSURLRequest *request = [NSURLRequest requestWithURL:imageUrl]; //2 connection sendasync異步請求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { //UIImage *image = [UIImage imageWithData:data]; //[imageView setImage:image]; //將網(wǎng)絡數(shù)據(jù)保存至緩存圖像。 v.cacheImage = [UIImage imageWithData:data]; //刷新表格 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }]; } #pragma mark 處理json數(shù)據(jù) - (void)handlerJSONData:(NSData *)data { //json文件中的[]表示一個數(shù)據(jù)。 //反序列化json數(shù)據(jù) //第二個參數(shù)是解析方式,一般用NSJSONReadingAllowFragments NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@", array); //json解析以后是nsarray格式的數(shù)據(jù)。 //提示:如果開發(fā)網(wǎng)絡應用,可以將反序列化出來的對象,保存至沙箱,以便后續(xù)開發(fā)使用。 NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [docs[0]stringByAppendingPathComponent:@"json.plist"]; [array writeToFile:path atomically:YES]; //把array里面的數(shù)據(jù)寫入沙箱中的jspn.plist中。 //給數(shù)據(jù)列表賦值 NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in array) { Video *video = [[Video alloc]init]; //給video賦值 [video setValuesForKeysWithDictionary:dict]; [arrayM addObject:video]; } self.dataList = arrayM; //刷新表格 [self.tableView reloadData]; NSLog(@"%@", arrayM); //這句話將調(diào)用video里面的description和nsarray+log里面的descriptionWithLocale } #pragma mark 加載json - (void)loadJson { NSLog(@"load json"); //從web服務器加載數(shù)據(jù) NSString *str = @"http://www.baidu.com?format=json"; //這里是亂寫的 //提示:NSData本身具有同步方法,但是在實際開發(fā)中,不要使用次方法 //在使用NSData的同步方法時,無法指定超時時間,如果服務器連接不正常,會影響用戶體驗。 //NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]]; //簡歷NSURL NSURL *url = [NSURL URLWithString:str]; //建立NSURLRequest NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:2.0f]; //建立NSURLConnect的同步方法加載數(shù)據(jù) NSURLResponse *response = nil; NSError *error = nil; //同步加載數(shù)據(jù) NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; //錯誤處理 if (data != nil) { //下面這兩句話本身沒有什么意義,僅用于跟蹤調(diào)試。 NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", result); //在處理網(wǎng)絡數(shù)據(jù)的時候,不要將NSData轉換成nsstring。 [self handlerJSONData:data]; }else if (data == nil && error == nil){ NSLog(@"空數(shù)據(jù)"); }else { NSLog(@"%@", error.localizedDescription); } } #pragma mark 加載xml - (void)loadXML { NSLog(@"load xml"); } //- (void)viewDidLoad //{ // [super viewDidLoad]; //} @end
相關文章
iOS開發(fā) widget構建詳解及實現(xiàn)代碼
這篇文章主要介紹了iOS開發(fā) widget構建詳解的相關資料,并附實例代碼,需要的朋友可以參考下2016-11-11ios uicollectionview實現(xiàn)橫向滾動
這篇文章主要為大家詳細介紹了ios uicollectionview實現(xiàn)橫向滾動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03簡介iOS開發(fā)中應用SQLite的模糊查詢和常用函數(shù)
這篇文章主要介紹了iOS開發(fā)中應用SQLite的模糊查詢和常用函數(shù),SQLite是一個可作嵌入式的數(shù)據(jù)庫非常適合小型應用使用,需要的朋友可以參考下2015-12-12