2016-08-26 22 views
0

我已經仿照我的API結果如下:JsonModel不能轉換陣列,JSON來jsonmodel繼承類

#import "PTPDestination.h" 
@interface PTPIndex : PTPBaseEntity 
@property (strong, nonatomic) NSNumber * citiesCount; 
@property (strong, nonatomic) NSNumber * hotelsCount; 
@property (strong, nonatomic) NSArray<PTPDestination *> * top_destinations; 
@end 

我也仿照PTPDestination這樣的:

@interface PTPDestination : PTPBaseEntity 
@property (assign, nonatomic) NSNumber * id; 
@property (assign, nonatomic) NSNumber * min_price; 
@property (assign, nonatomic) NSNumber * max_discount; 
@property (assign, nonatomic) NSString * title; 
@end 

,我打電話給我的API與AFNetworking是這樣的:

AFHTTPSessionManager * manager = [self createAPISessionManager]; 
[manager GET:[self createServiceUrlWithMethod:@"someURL"] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { 
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 
    NSError * error = nil; 
    PTPIndex * index = [[PTPIndex alloc] initWithDictionary:responseObject error:&error]; 
    if (error) { 
     callback (nil, [PTPApiCenteralizedErrorHandler getErrorFromApiError:error task:task responseObject:responseObject]); 
     return; 
    } 
    callback (index, nil); 
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
    callback (nil, [PTPApiCenteralizedErrorHandler getErrorFromApiError:error task:task]); 
}]; 

問題是與目標數組。我不知道爲什麼數組未被轉換爲PTPDestination對象,並且它仍然是一個NSDictionaries數組。

爲什麼會發生這種情況,我如何獲得我的自定義類的數組?

+0

您是否有從服務器返回的JSON有效負載的示例? –

+0

@CraigOtis我稱之爲網址==> www.pintapin.com/service/index/mobile – aakpro

+0

「PTPBaseEntity」是什麼樣的?它是否也擴展'JSONModel'? –

回答

0

不,JSON模型也將數組轉換爲JSONObject,如果你想訪問PTPDestination類的屬性。

PTPIndex類

#import "JSONModel.h" 
@interface PTPIndex : JSONModel 
@property (strong, nonatomic) NSNumber * citiesCount; 
@property (strong, nonatomic) NSNumber * hotelsCount; 
@property (strong, nonatomic) NSArray<PTPDestination *> * top_destinations; 
@end 

PPTPDestination類

#import "JSONModel.h" 
@interface PTPDestination : JSONModel 
@property (assign, nonatomic) NSNumber * id; 
@property (assign, nonatomic) NSNumber * min_price; 
@property (assign, nonatomic) NSNumber * max_discount; 
@property (assign, nonatomic) NSString * title; 
@end 

的NSDictionary 「數據」 從網絡響應

PTPIndex *ptpInd = [[PTPIndex alloc] initWithDictionary:data error:&error]; 

找到PTPDestination的總數,並在循環中運行。 您可以像這樣訪問對象。

PTPDestination *ptpdest = [ptpInd PTPDestination[0]];