RestKit 对象映射问题

Posted

技术标签:

【中文标题】RestKit 对象映射问题【英文标题】:RestKit Object mapping issue 【发布时间】:2014-01-09 12:11:58 【问题描述】:

我有这个 JSON 响应,但我不知道如何映射。它看起来像这样:


  "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。

【问题讨论】:

您是否在某处设置了响应描述符? 我只是不知道如何正确设置RKObjectMappingRKResponseDescriptor。这是我的问题。 【参考方案1】:

因此,为了设置映射,您首先需要定义 API 的基本 url,如下所示:

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

那么你需要为上面输出json的url设置一个响应描述符:

[[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 提供的演示。这使得整个过程更加清晰。

干杯, 塞巴斯蒂安

【讨论】:

但是路径不是/sideMenuList。 URL 是http://my.website.com/service/service.svc/screens/mainScreenData,里面是我在我的问题上发布的数组。 那么你需要相应地改变它。将基 URL 设置为任何基数。我假设:my.website.com/service/service.svc 然后将路径设置为 /screens/mainScreenData 这不是我的问题,我知道该怎么做。你读过我的问题吗?我的问题是如何以正确的方式映射对象... @YossiTsafar,您已经创建了映射,Sebastian 正在向您展示如何使用它们。如果您仍然有问题,您需要更清楚地了解它们是什么。您应该显示实际的 JSON,而不是一些日志输出或类似内容。您需要列出您收到的错误消息... 我已经编辑了我的问题并添加了来自服务器的 JSON 响应,请查看。我只是不知道映射这些对象的正确方法是什么。谢谢你们俩。

以上是关于RestKit 对象映射问题的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 更新对象映射空属性

Restkit 0.20 嵌套对象数组映射问题

RestKit - 没有对象映射的请求

无法映射嵌套的 RestKit 对象

RestKit 仅用于对象映射

RestKit 不喜欢为 POST 响应映射不同的对象