restkit嵌套对象与数组映射

时间:2014-03-11 21:43:28

标签: objective-c json restkit

我有这个json:

{
    "status": "ok",
    "post": {
        "id": 171,
        "type": "locations",
        "slug": "plaza-404",
        "url": "http://localhost/?locations=plaza-404",
        "status": "publish",
        "title": "Sucursal Plaza 404",
        "title_plain": "Sucursal Plaza 404",
        "content": "",
        "excerpt": "",
        "date": "2014-03-05 19:59:50",
        "modified": "2014-03-07 19:11:43",
        "categories": [],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "arturocalvo",
            "name": "arturocalvo",
            "first_name": "",
            "last_name": "",
            "nickname": "arturocalvo",
            "url": "",
            "description": ""
        },
        "comments": [],
        "attachments": [],
        "comment_count": 0,
        "comment_status": "closed",
        "custom_fields": {
            "id": [
                "1"
            ],
            "location_id": [
                 "1"
            ],
            "calle": [
                "Av. Gomez Morin No.404  Local A3-5"
            ]
}

我想只将标题映射到名为“calle”的自定义字段,这是我的位置界面

@interface Location : NSObject

@property (nonatomic, copy) NSString * name;
@property (nonatomic, copy) NSSet * calle;
@end

这是我的代码

RKObjectMapping *locationMapping = [RKObjectMapping mappingForClass:[Location class]];
RKObjectMapping *locationCustomMapping = [RKObjectMapping mappingForClass: [LocationCustom class]];

[locationMapping addAttributeMappingsFromDictionary:@{
                                                      @"title": @"name",
                                                      @"calle": @"calle"
                                                      }];


RKResponseDescriptor *locationResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:locationMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"post" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:locationResponseDescriptor];


NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc]    initWithRequest:request responseDescriptors:@[ locationResponseDescriptor ]];

但由于某种原因,我无法映射它,每个时间都会得到一个空值,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

您缺少JSON中需要告诉RestKit导航的级别。使用:

@"custom_fields.calle": @"calle"

在你的地图中。