2014-01-09 298 views
0

我有這個JSON響應,我無法弄清楚如何映射。它看起來像這樣:RestKit對象映射問題

{ 
    "responseError": null, 
    "displayMenuList": { 
    "MenuList": [ 
     { 
     "ID": "3223", 
     "Name": "Main", 
     "AddressURL": "www.mysite.com", 
     "DisplayType": "True", 
     "ImageURL": "main.png", 
     "NotSplitBUser": "True", 
     "ParentCategoryId": "3223", 
     "PrivateUser": "True", 
     "SortOrder": 1, 
     "SplitBUser": "True", 
     "TargetURL": "_self" 
     }, 
     { 
     "ID": "3307", 
     "Name": "Contact", 
     "AddressURL": "www.mysite.com", 
     "DisplayType": "True", 
     "ImageURL": "service.png", 
     "NotSplitBUser": "True", 
     "ParentCategoryId": "3224", 
     "PrivateUser": "True", 
     "SortOrder": 0, 
     "SplitBUser": "True", 
     "TargetURL": "_self" 
     }, 
     { 
     "ID": "3298", 
     "Name": "Call Us", 
     "AddressURL": "www.mysite.com", 
     "DisplayType": "True", 
     "ImageURL": "service.png", 
     "NotSplitBUser": "True", 
     "ParentCategoryId": "3224", 
     "PrivateUser": "True", 
     "SortOrder": 1, 
     "SplitBUser": "True", 
     "TargetURL": "_self" 
     }, 
     { 
     "ID": "3224", 
     "Name": "Service", 
     "AddressURL": "www.mysite.com", 
     "DisplayType": "True", 
     "ImageURL": "service.png", 
     "NotSplitBUser": "True", 
     "ParentCategoryId": "3224", 
     "PrivateUser": "True", 
     "SortOrder": 2, 
     "SplitBUser": "True", 
     "TargetURL": "_self" 
     } 
    ] 
    }, 
    "responseCurrentBillState": null, 
    "responseGetPcrfSubBuckets": null, 
    "userData": { 
    "abroadInd": null, 
    "accountType": "B", 
    "customerId": "", 
    "fullName": "Juan", 
    "subscriberNumber": "" 
    } 
} 

我只是無法弄清楚如何將這些對象映射,我創建對象調用RKSideMenu,也是一個對象稱爲RKUserData他們看起來像這樣:

@interface RKSideMenu : NSObject 
@property (copy, nonatomic) NSString *addressURL; 
@property (copy, nonatomic) NSString *displayType; 
@property (copy, nonatomic) NSNumber *id_number; 
@property (copy, nonatomic) NSString *imageURL; 
@property (copy, nonatomic) NSString *name; 
@property (assign, nonatomic) BOOL splitBUser; 
+ (NSDictionary*)getAttributes; 
@end 

@implementation RKSideMenu 
+ (NSDictionary*)getAttributes 
{ 
    return [NSDictionary dictionaryWithObjects:@[@"addressURL", @"displayType", @"id_number", @"imageURL", @"name", @"splitBUser"] 
             forKeys:@[@"AddressURL", @"DisplayType", @"ID", @"ImageURL", @"Name", @"SplitBUser"]]; 
} 
@end 

@interface RKUserData : NSObject 
@property (copy, nonatomic) NSString *abroadInd; 
@property (copy, nonatomic) NSString *accountType; 
@property (copy, nonatomic) NSString *customerID; 
@property (copy, nonatomic) NSString *fullName; 
@property (copy, nonatomic) NSString *subscriberNumber; 
+ (NSDictionary*)getAttributes; 
@end 

@implementation RKUserData 
+ (NSDictionary*)getAttributes 
{ 
    return [NSDictionary dictionaryWithObjects:@[@"abroadInd", @"accountType", @"customerID", @"fullName;", @"subscriberNumber"] 
             forKeys:@[@"abroadInd", @"accountType", @"customerId", @"fullName;", @"subscriberNumber"]]; 
} 
@end 

我開始繪製這兩種方法的映射,但是我堅持不知道該怎麼做。我看着https://github.com/RestKit/RestKit/wiki/Object-mapping,但仍然沒有把握好。

RKObjectMapping *sideMenuMapping = [RKObjectMapping mappingForClass:[RKSideMenu class]]; 
[sideMenuMapping addAttributeMappingsFromDictionary:[RKSideMenu getAttributes]]; 

RKObjectMapping *userDataMapping = [RKObjectMapping mappingForClass:[RKUserData class]]; 
[userDataMapping addAttributeMappingsFromDictionary:[RKUserData getAttributes]]; 

在此先感謝!

編輯:Json在頂部從服務器取代真正的JSON。

+0

你設置一個響應描述的地方? –

+0

我只是不知道如何設置'RKObjectMapping'和'RKResponseDescriptor'正確的方式..這是我的問題。 –

回答

1

所以你需要爲了設置做你的映射是首先定義您的API的基礎URL,就像這樣:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"URL_TO_YOUR_API"]]; 
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; 

然後,你需要設置的URL的響應描述符輸出上面的json:

[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:sideMenuMapping 
                            method:RKRequestMethodAny 
                           pathPattern:@"/mainScreenData" 
                            keyPath:@"displayMenuList.MenuList" 
                           statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]; 

「RKRequestMethodAny」應替換爲您正在使用的請求方法,例如「RKRequestMethodGET」。

然後你只需通過調用檢索對象:

[[RKObjectManager sharedManager] getObjectsAtPath:@"/mainScreenData" 
        parameters:nil 
         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

          NSArray *sideMenuList = [NSMutableArray arrayWithArray:[mappingResult array]]; 
         } 
         failure:^(RKObjectRequestOperation *operation, NSError *error) { 
          NSLog(@"%@",[error localizedDescription]); 
         }]; 

我會強烈建議你在看看由RestKit提供的演示。這使整個過程更清晰。

乾杯,
塞巴斯蒂安

+0

但路徑不是/ sideMenuList。 URL是'http:// my.website.com/service/service.svc/screens/mainScreenData',裏面是我發佈在我的問題上的數組。 –

+0

那麼你需要相應地改變它。將基本Url設置爲任何基礎。我會假設:http://my.website.com/service/service.svc,然後將路徑設置爲/ screens/mainScreenData –

+0

這不是我的問題,我知道該怎麼做。你有沒有讀過我的問題?我的問題是如何以正確的方式映射對象... –