本節學習內容:
1.json 數據的定義
2.json 數據的解析方法
3.json數據解析實踐
SBJsonParser: JSON數據解析類
objectWithString:jsonString:解析字符串數據
NSJSONSerialization: ios JSON解析類
JSONObjectWithData:通過二進制解析數據
導入DouBan.json文件
【ViewController.h】
#import<UIKit/UIKit.h>
@interface ViewController:UIViewController
//數據視圖代理協議
<UITableViewDataSource,UITablViewDalegate>{
//數據視圖定議
UITableView* _tableView;
NSMutableArray* _arrayBooks;
}
@end
【viewController.m】
#import"viewController.h"
#import"BookModel.h"
@interface ViewController()
@end
@implementation ViewController
-(void)viewDidLoad{
[super viewDidLoad];
//創建數據視圖
_tableView=[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrounped];
_tableView.delegate=self;
_tableView.dataSource=self;
[self.view addSubview:_tableView];
//獲取組數
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
returen 1;
}
//獲取每組的行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arrayBooks.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView celllForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* strID=@"ID";
UITableViewCell* cell=[_tableView dequeueReusableCellWithIdentifier:strId];
if(cell == nil){
//創建數據源單元格對象
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strID];
}
//獲取對應的圖書數據
BookMode* book=[_arrayBooks objectAtIndex:indexPath.row];
//將數名賦值給文本label
cell.textLable.text=book.mBookName;
cell.detailTextLabel.text=book.mPrice;
return cell;
}
//解析數據
-(void)parseData{
//獲取json文件本地路徑
NSString* path=[[NSBundle mainBundle]pathForResource:@"DouBan" ofType:@"json"];
//加載json文件為二進制文件
NSData* data=[NSData dataWithContentsOfFile:path];
//字典對象解析,將json數據解析為字典格式
NSDictionary* dicRoot=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//判斷解析是否為字典
if([dicRoot isKingOfClass:[NSDictionar class]]){
//開始解析數據
_arrayBooks=[[NSMutableArray alloc]init];
//解析根數據
NSArray* arrayEnter=[dicRoot objectForKey:@"entry"];
//遍歷數組對象
for(init i=0;i<arrayEnter.count;i++){
NSDictionary* dicBook=[arrayEnter objectAtIndex:i];
//獲取書籍名字對象
NSDictionary*? bookNmeDic=[dicRoot objectForKey:@"title"];
NSString* bookName=[bookNameDic bojectForKey:@"$t"];
BookModel* book=[[BookModel alloc]init];
book.mBookName=bookName;
//數組對象
NSArray* arrAttr=[dicBook objectForKey:@"db:attribute"];
for(int i=0;i<arrAttr.count;i++){
NSDictionary* dic=[arrAttr objectAtIndex:i];
NSString* strNAme=[dic objectForKey:@"@name"];
if([strName isEqualToString:@"price")]==YES){
NSString* price=[dic objectForKey:@"st"];
book.mPrice=price;
}else if(([strName isEqualToString:@"publisher"]==YES){
book.mPublisher=pub;
}
}
//添加到數組中
[_arrayBooks addObject:book];
}
}
//更新數據視圖數據
[_tableView reloadData]
}
【BookModel.h】
#import<Foundation/Foundation>
//書籍的數據模型
@interface BookMode:NSObject{
//書籍名稱
NSString* _bookName;
//出版社名稱
NSString* _publisher;
//書籍價格
NSString* _price;
//作者數組
NSMutableArray* _authorArray;
}
@property(retain,nonatomic) NSString* mBookName;
@property(retain,nonatomic) NSString* mPublisher;
@property(retain,nonatomic) NSString* mPrice;
@property(retain,nonatomic) NSString* mAuthorArray;
@end
【BookModel.m】
#import "BookModel.h"
@implementation BookModel
@syntesize mBookName=_bookName;
@syntesize mAuthorArray=_mAuthorArray;
@syntesize mPrice=_mPrice;
@syntesize mPublisher=_mPublisher;
@end;