Json資料解析後分類思路

2021-06-15 04:28:04 字數 2606 閱讀 9331

json資料解析後分類思路

"大字典 2.zip"

},...

],"previous_cursor": 0, // 暫未支援

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

"total_number": 81655

}

我來看一下**如何實現的:

新建user類用來存放user字典中的內容。

.h檔案如下:

#import @inte***ce user : nsobject

@property (strong, nonatomic) nsstring* screen_name;

-(user*)initwithjsondictionary:(nsdictionary*)dic;

+(user*)userwithjsondictionary:(nsdictionary*)dic;

@end

.m檔案如下:

@implementation user

-(user*)initwithjsondictionary:(nsdictionary *)dic

return self;

}+(user*)userwithjsondictionary:(nsdictionary *)dic

新建status類用來存放statuses字典中的內容。

.h檔案如下:

#import #import "user.h"

@inte***ce status : nsobject

@property (strong, nonatomic) nsstring* userid;

//將多個字典巢狀的資料取出的思路是為每乙個字典對應的建乙個資料模型的類

//例如:user類

@property (strong , nonatomic) user* user;

-(status*)initwithjsondictionary:(nsdictionary*)dic;

+(status*)statuswithjsondictionary:(nsdictionary*)dic;

@end

.m檔案如下:

@implementation status

-(status*)initwithjsondictionary:(nsdictionary *)dic

}return self;

}+(status*)statuswithjsondictionary:(nsdictionary *)dic

為了模擬在真實專案中,獲得資料的類和顯示資料的類不在乙個類中

我們新建乙個試圖控制器用來顯示資料,我們顯示資料的位置是在控制台。

otherviewcontroller.h**如下:

#import @inte***ce otherviewcontroller : uiviewcontroller

@property (strong , nonatomic) nsarray* statusarr;

@end

otherviewcontroller.m**實現如下:

//用來接收通過訊息機制傳送來的資料

-(void)getarray:(nsnotification*)anotification

- (void)viewdidload

-(void)displayuserinfo

最後,我們來看一下最後的乙個類,我們從這個類中獲取資料,獲取資料的本地檔案在**例子中。

viewcontroller.h**如下:

#import @inte***ce viewcontroller : uiviewcontroller

-(ibaction)changevc:(id)sender;

@end

viewcontroller.m檔案的**實現如下:

需要在xib中拖乙個button與下面的方法相關聯

-(ibaction)changevc:(id)sender

//將temparr陣列通過訊息中心傳送到@"getarray" 這個名字的訊息對應的方法中

[[nsnotificationcenter defaultcenter] postnotificationname:@"getarray" object:temparr];

//切換檢視控制器

[[nsnotificationcenter defaultcenter] postnotificationname:@"changevc" object:nil];

}

本文出自 「7087095」 部落格,請務必保留此出處

json資料解析

由於是從網路上面讀取json,因此需要讀取網路檔案 url url new url path httpurlconnection conn httpurlconnection url.openconnection 利用httpurlconnection物件,我們可以從網路中獲取網頁資料.conn.s...

json資料解析

今天在做easyui列表頁面回顯字典表資料時候遇到乙個json解析的奇怪問題,首先我用spring gethibernatetemplate 中的createsqlquery進行資料查詢 查出來的資料是 2,2,2,2,2,2,2,之類的 public list findbypage final s...

Json 資料解析

假設現在要建立這樣乙個json文字 物件 married false 布林值 try 新建people物件 jsonarray phone new jsonarray phone是個陣列 phone.put 123231 put 1231234 把 號碼放到phone裡面 people.put ph...