【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Json數(shù)據(jù)解析后分類思路_第1頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Json數(shù)據(jù)解析后分類思路_第2頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Json數(shù)據(jù)解析后分類思路_第3頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Json數(shù)據(jù)解析后分類思路_第4頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Json數(shù)據(jù)解析后分類思路_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余2頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Json數(shù)據(jù)解析后分類思路

Json數(shù)據(jù)解析后分類思路代碼下載地址:

"大字典2.zip"/s/HzjOj我們這里已從新浪微博中請(qǐng)求回來(lái)的數(shù)據(jù)作為例子。為了讓工程簡(jiǎn)化,我將數(shù)據(jù)寫入到本地了。這里主要是為了學(xué)習(xí)如何將Json數(shù)據(jù)解析分類。新浪微博請(qǐng)求返回來(lái)的數(shù)據(jù)大致格式如下:{

"statuses":[

{

"created_at":"TueMay3117:46:55+08002011",

"id":11488058246,

"text":"求關(guān)注。",

"source":"<ahref=""rel="nofollow">新浪微博</a>",

"favorited":false,

"truncated":false,

"in_reply_to_status_id":"",

"in_reply_to_user_id":"",

"in_reply_to_screen_name":"",

"geo":null,

"mid":"5612814510546515491",

"reposts_count":8,

"comments_count":9,

"annotations":[],

"user":{

"id":1404376560,

"screen_name":"zaku",

"name":"zaku",

"province":"11",

"city":"5",

"location":"北京朝陽(yáng)區(qū)",

"description":"人生五十年,乃如夢(mèng)如幻;有生斯有死,壯士復(fù)何憾。",

"url":"/zaku",

"profile_p_w_picpath_url":"/1404376560/50/0/1",

"domain":"zaku",

"gender":"m",

"followers_count":1204,

"friends_count":447,

"statuses_count":2908,

"favourites_count":0,

"created_at":"FriAug2800:00:00+08002009",

"following":false,

"allow_all_act_msg":false,

"remark":"",

"geo_enabled":true,

"verified":false,

"allow_all_comment":true,

"avatar_large":"/1404376560/180/0/1",

"verified_reason":"",

"follow_me":false,

"online_status":0,

"bi_followers_count":215

}

},

...

],

"previous_cursor":0,//暫未支持

"next_cursor":11488013766,//暫未支持

"total_number":81655

}以上的示例來(lái)自新浪微博官網(wǎng)。我標(biāo)出的紅色字體,是我們JSon解析分類的依據(jù),他們都是字典,也就是說(shuō)JSon解析分類的思路是按照字典去新建類,類與類之間的嵌套關(guān)系和JSon數(shù)據(jù)的格式相同,這我JSon解析的方法。我來(lái)看一下代碼如何實(shí)現(xiàn)的:新建User類用來(lái)存放user字典中的內(nèi)容。.h文件如下:#import<Foundation/Foundation.h>

@interfaceUser:NSObject

@property(strong,nonatomic)NSString*screen_name;

-(User*)initWithJsonDictionary:(NSDictionary*)dic;

+(User*)UserWithJsonDictionary:(NSDictionary*)dic;

@end.m文件如下:@implementationUser

-(User*)initWithJsonDictionary:(NSDictionary*)dic

{

if(self=[superinit]){

self.screen_name=[dicobjectForKey:@"screen_name"];

}

returnself;

}

+(User*)UserWithJsonDictionary:(NSDictionary*)dic

{

//用這個(gè)類方法進(jìn)行初始化的時(shí)候,都會(huì)alloc一次,因此就會(huì)新分配一塊空間

return[[Useralloc]initWithJsonDictionary:dic];

}新建Status類用來(lái)存放statuses字典中的內(nèi)容。.h文件如下:#import<Foundation/Foundation.h>

#import"User.h"

@interfaceStatus:NSObject

@property(strong,nonatomic)NSString*userID;

//將多個(gè)字典嵌套的數(shù)據(jù)取出的思路是為每一個(gè)字典對(duì)應(yīng)的建一個(gè)數(shù)據(jù)模型的類

//例如:User類

@property(strong,nonatomic)User*user;

-(Status*)initWithJsonDictionary:(NSDictionary*)dic;

+(Status*)statusWithJsonDictionary:(NSDictionary*)dic;

@end.m文件如下:@implementationStatus

-(Status*)initWithJsonDictionary:(NSDictionary*)dic

{

if(self=[superinit]){

self.userID=[dicobjectForKey:@"idstr"];

NSDictionary*userDic=[dicobjectForKey:@"user"];

if(userDic){

self.user=[UserUserWithJsonDictionary:userDic];

}

}

returnself;

}

+(Status*)statusWithJsonDictionary:(NSDictionary*)dic

{

//用這個(gè)類方法進(jìn)行初始化的時(shí)候,都會(huì)alloc一次,因此就會(huì)新分配一塊空間

return[[Statusalloc]initWithJsonDictionary:dic];

}為了模擬在真實(shí)項(xiàng)目中,獲得數(shù)據(jù)的類和顯示數(shù)據(jù)的類不在一個(gè)類中我們新建一個(gè)試圖控制器用來(lái)顯示數(shù)據(jù),我們顯示數(shù)據(jù)的位置是在控制臺(tái)。OtherViewController.h代碼如下:#import<UIKit/UIKit.h>

@interfaceOtherViewController:UIViewController

@property(strong,nonatomic)NSArray*statusArr;

@endOtherViewController.m代碼實(shí)現(xiàn)如下://用來(lái)接收通過(guò)消息機(jī)制發(fā)送來(lái)的數(shù)據(jù)

-(void)getArray:(NSNotification*)aNotification

{

self.statusArr=aNotification.object;

}-(void)viewDidLoad

{

[superviewDidLoad];

//Doanyadditionalsetupafterloadingtheview.

//添加一個(gè)按鈕點(diǎn)擊顯示數(shù)據(jù)

UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

btn.frame=CGRectMake(30,30,30,30);

[self.viewaddSubview:btn];

[btnaddTarget:selfaction:@selector(displayUserInfo)forControlEvents:UIControlEventTouchUpInside];

}-(void)displayUserInfo

{

Status*status=[self.statusArrobjectAtIndex:0];

NSLog(@"status.userID=%@",status.userID);

NSLog(@"status.user.screen_name=%@",status.user.screen_name);

}最后,我們來(lái)看一下最后的一個(gè)類,我們從這個(gè)類中獲取數(shù)據(jù),獲取數(shù)據(jù)的本地文件在代碼例子中。ViewController.h代碼如下:#import<UIKit/UIKit.h>

@interfaceViewController:UIViewController

-(IBAction)changeVC:(id)sender;

@endViewController.m文件的代碼實(shí)現(xiàn)如下:需要在Xib中拖一個(gè)Button與下面的方法相關(guān)聯(lián)-(IBAction)changeVC:(id)sender

{

//將JSON格式的數(shù)據(jù)文件的路徑找出

NSString*path=[[NSBundlemainBundle]pathForResource:@"jsonTest"ofType:@"json"];

//將數(shù)據(jù)放入NSData中

NSData*data=[NSDatadataWithContentsOfFile:path];

//JSON解析

NSDictionary*dic=[NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableLeaveserror:nil];

//NSLog(@"dic=%@",dic);

NSArray*arr=[dicobjectForKey:@"statuses"];

NSLog(@"%d",arr.count);

NSLog(@"arr=%@",arr);

//初始化一個(gè)數(shù)組

NSMutableArray*tempArr=[NSMutableArrayarray];

//將數(shù)組中的字典放入Status模型中,大家在這里會(huì)有一個(gè)疑問(wèn)將數(shù)組中的字典都放入模型中,怎么區(qū)分不同數(shù)組中的數(shù)據(jù)?

for(NSDictionary*iteminarr){

//答案在statusWithJsonDictionary:的類方法中見(jiàn)該方法注釋

Status*status=[StatusstatusWithJsonDictionar

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論