Json數據解析後分類思路
代碼下載地址: "大字典 2.zip"
http://vdisk.weibo.com/s/HzjOj
我們這裏已從新浪微博中請求回來的數據作爲例子。爲了讓工程簡化,我將數據寫入到本地了。這裏主要是爲了學習如何將Json數據解析分類。
新浪微博請求返回來的數據大致格式如下:
以上的示例來自新浪微博官網。我標出的紅色字體 ,是我們JSon解析分類的依據,他們都是字典,也就是說JSon解析分類的思路是按照字典去新建類,類與類之間的嵌套關係和JSon數據的格式相同,這我JSon解析的方法。
我來看一下代碼如何實現的:
新建User類用來存放user字典中的內容。
.h文件如下:
#import <Foundation/Foundation.h> @interface User : NSObject @property (strong, nonatomic) NSString* screen_name; -(User*)initWithJsonDictionary:(NSDictionary*)dic; +(User*)UserWithJsonDictionary:(NSDictionary*)dic; @end
.m文件如下:
@implementation User -(User*)initWithJsonDictionary:(NSDictionary *)dic { if (self = [super init]) { self.screen_name = [dic objectForKey:@"screen_name"]; } return self; } +(User*)UserWithJsonDictionary:(NSDictionary *)dic { //用這個類方法進行初始化的時候,都會alloc一次,因此就會新分配一塊空間 return [[User alloc] initWithJsonDictionary:dic]; }
新建Status類用來存放statuses字典中的內容。
.h文件如下:
#import <Foundation/Foundation.h> #import "User.h" @interface 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 { if (self = [super init]) { self.userID = [dic objectForKey:@"idstr"]; NSDictionary* userDic = [dic objectForKey:@"user"]; if (userDic) { self.user = [User UserWithJsonDictionary:userDic]; } } return self; } +(Status*)statusWithJsonDictionary:(NSDictionary *)dic { //用這個類方法進行初始化的時候,都會alloc一次,因此就會新分配一塊空間 return [[Status alloc] initWithJsonDictionary:dic]; }
爲了模擬在真實項目中,獲得數據的類和顯示數據的類不在一個類中
我們新建一個試圖控制器用來顯示數據,我們顯示數據的位置是在控制檯。
OtherViewController.h代碼如下:
#import <UIKit/UIKit.h> @interface OtherViewController : UIViewController @property (strong , nonatomic) NSArray* statusArr; @end
OtherViewController.m代碼實現如下:
//用來接收通過消息機制發送來的數據 -(void)getArray:(NSNotification*)aNotification { self.statusArr = aNotification.object; }
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //添加一個按鈕點擊顯示數據 UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(30, 30, 30, 30); [self.view addSubview:btn]; [btn addTarget:self action:@selector(displayUserInfo) forControlEvents:UIControlEventTouchUpInside]; }
-(void)displayUserInfo { Status* status = [self.statusArr objectAtIndex:0]; NSLog(@"status.userID = %@",status.userID); NSLog(@"status.user.screen_name = %@",status.user.screen_name); }
最後,我們來看一下最後的一個類,我們從這個類中獲取數據,獲取數據的本地文件在代碼例子中。
ViewController.h代碼如下:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController -(IBAction)changeVC:(id)sender; @end
ViewController.m文件的代碼實現如下:
需要在Xib中拖一個Button與下面的方法相關聯
-(IBAction)changeVC:(id)sender { //將JSON格式的數據文件的路徑找出 NSString* path = [[NSBundle mainBundle] pathForResource:@"jsonTest" ofType:@"json"]; //將數據放入NSData中 NSData* data = [NSData dataWithContentsOfFile:path]; //JSON解析 NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; // NSLog(@"dic = %@",dic); NSArray* arr = [dic objectForKey:@"statuses"]; NSLog(@"%d",arr.count); NSLog(@"arr = %@",arr); //初始化一個數組 NSMutableArray* tempArr = [NSMutableArray array]; //將數組中的字典放入Status模型中,大家在這裏會有一個疑問將數組中的字典都放入模型中,怎麼區分不同數組中的數據? for (NSDictionary* item in arr) { //答案在statusWithJsonDictionary:的類方法中見該方法註釋 Status* status = [Status statusWithJsonDictionary:item]; NSLog(@"item = %@ ",item); //將Status類型的對象放入數組中 [tempArr addObject:status]; } //將tempArr數組通過消息中心發送到@"getArray" 這個名字的消息對應的方法中 [[NSNotificationCenter defaultCenter] postNotificationName:@"getArray" object:tempArr]; //切換視圖控制器 [[NSNotificationCenter defaultCenter] postNotificationName:@"changeVC" object:nil]; }