本節(jié)學(xué)習(xí)內(nèi)容:
1.json 數(shù)據(jù)的定義
2.json 數(shù)據(jù)的解析方法
3.json數(shù)據(jù)解析實(shí)踐
SBJsonParser: JSON數(shù)據(jù)解析類
objectWithString:jsonString:解析字符串?dāng)?shù)據(jù)
NSJSONSerialization: ios JSON解析類
JSONObjectWithData:通過二進(jìn)制解析數(shù)據(jù)
導(dǎo)入DouBan.json文件
【ViewController.h】
#import<UIKit/UIKit.h>
@interface ViewController:UIViewController
//數(shù)據(jù)視圖代理協(xié)議
<UITableViewDataSource,UITablViewDalegate>{
//數(shù)據(jù)視圖定議
UITableView* _tableView;
NSMutableArray* _arrayBooks;
}
@end
【viewController.m】
#import"viewController.h"
#import"BookModel.h"
@interface ViewController()
@end
@implementation ViewController
-(void)viewDidLoad{
[super viewDidLoad];
//創(chuàng)建數(shù)據(jù)視圖
_tableView=[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrounped];
_tableView.delegate=self;
_tableView.dataSource=self;
[self.view addSubview:_tableView];
//獲取組數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
returen 1;
}
//獲取每組的行數(shù)
-(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){
//創(chuàng)建數(shù)據(jù)源單元格對(duì)象
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strID];
}
//獲取對(duì)應(yīng)的圖書數(shù)據(jù)
BookMode* book=[_arrayBooks objectAtIndex:indexPath.row];
//將數(shù)名賦值給文本label
cell.textLable.text=book.mBookName;
cell.detailTextLabel.text=book.mPrice;
return cell;
}
//解析數(shù)據(jù)
-(void)parseData{
//獲取json文件本地路徑
NSString* path=[[NSBundle mainBundle]pathForResource:@"DouBan" ofType:@"json"];
//加載json文件為二進(jìn)制文件
NSData* data=[NSData dataWithContentsOfFile:path];
//字典對(duì)象解析,將json數(shù)據(jù)解析為字典格式
NSDictionary* dicRoot=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//判斷解析是否為字典
if([dicRoot isKingOfClass:[NSDictionar class]]){
//開始解析數(shù)據(jù)
_arrayBooks=[[NSMutableArray alloc]init];
//解析根數(shù)據(jù)

NSArray* arrayEnter=[dicRoot objectForKey:@"entry"];
//遍歷數(shù)組對(duì)象
for(init i=0;i<arrayEnter.count;i++){
NSDictionary* dicBook=[arrayEnter objectAtIndex:i];
//獲取書籍名字對(duì)象
NSDictionary*? bookNmeDic=[dicRoot objectForKey:@"title"];
NSString* bookName=[bookNameDic bojectForKey:@"$t"];
BookModel* book=[[BookModel alloc]init];
book.mBookName=bookName;
//數(shù)組對(duì)象
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;
}
}
//添加到數(shù)組中
[_arrayBooks addObject:book];
}
}
//更新數(shù)據(jù)視圖數(shù)據(jù)
[_tableView reloadData]
}
【BookModel.h】
#import<Foundation/Foundation>
//書籍的數(shù)據(jù)模型
@interface BookMode:NSObject{
//書籍名稱
NSString* _bookName;
//出版社名稱
NSString* _publisher;
//書籍價(jià)格
NSString* _price;
//作者數(shù)組
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;
