IOS開發(fā) JSON數(shù)據(jù)解析

本節(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;


運(yùn)行結(jié)果
最后編輯于
?著作權(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)容

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