Restkit 返回具有 0 个键值对的对象数组

Posted

技术标签:

【中文标题】Restkit 返回具有 0 个键值对的对象数组【英文标题】:Restkit returning array of objects with 0 key value pairs 【发布时间】:2016-01-15 19:46:09 【问题描述】:

我不确定我在这里缺少什么。对于这个请求,我得到一个带有“data.file”键路径的 JSON 响应。响应描述符是这样设置的:

[fileMapping addAttributeMappingsFromDictionary:@@"parentPath" : @"parentPath"];

RKResponseDescriptor *fileResponseDescriptorFile =
    [RKResponseDescriptor responseDescriptorWithMapping:fileMapping
                    method:RKRequestMethodAny
               pathPattern:nil
                   keyPath:@"data.file"
               statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

getObjectsAtPath 调用的成功块被触发,从那里映射results.array 显示一个对象数组,但没有键值对。

如果我手动反序列化 JSON,我会正确填充键值对。

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:operation.HTTPRequestOperation.responseData options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *data=[json objectForKey:@"data"];
        NSArray *files = [data objectForKey:@"file"];
        NSLog(@"files: %@", files);

这是 JSON 的 sn-p:


"data": 
    "folder": [
        
            "parentPath": "xxx",
            "name": "xxx",
            "versionCreated": "2015-12-11T19:26:19.782Z"
        
    ],
    "file": [
        
            "parentPath": "xxx",
            "name": "2015-09-18 104310-4377094.JPG",
            "size": 4377094,
            "checksum": "5988e769689095c6512a3b5382e92677e0aa495b8a2d71506d3ca24b561f2dff",
            "contentToken": "xxx",
            "versionCreated": "2015-09-18T14:53:36.525Z",
            "version": 1,
            "systemAttributes": 
                "Capture-Date": "2015-09-18T14:43:10.000Z",
                "Geo-Latitude": 40.00000,
                "Geo-Longitude": -74.00000,
                "Height": 3024,
                "Mime-Type": "image/jpeg",
                "Orientation": 6,
                "Timeline-Date": "2015-09-18T14:43:10.000Z",
                "Width": 4032
            
        ...

Restkit 日志记录:

CloudAPIRestKit[86320:8464807] D restkit.object_mapping:RKMapperOperation.m:414 Executing mapping operation for representation: 
    data =     
        file =         (
                        
                checksum = xxx;
                contentToken = "xxx-gmkQ4oOO1c~";
                name = "2015-09-18 104310-4377094.JPG";
                parentPath = "/xxx";
                size = 4377094;
                systemAttributes =                 
                    "Capture-Date" = "2015-09-18T14:43:10.000Z";
                    "Geo-Latitude" = "40.00000";
                    "Geo-Longitude" = "-74.00000";
                    Height = 3024;
                    "Mime-Type" = "image/jpeg";
                    Orientation = 6;
                    "Timeline-Date" = "2015-09-18T14:43:10.000Z";
                    Width = 4032;
                ;
                version = 1;
                versionCreated = "2015-09-18T14:53:36.525Z";
            ,...

 with mapping <RKObjectMapping:0x7fa958654190 objectClass=CloudFileModel propertyMappings=(
    "<RKAttributeMapping: 0x7fa958729b60 parentPath => parentPath>",
    "<RKAttributeMapping: 0x7fa958654500 name => name>"
)>
2016-01-18 09:50:31.262 CloudAPIRestKit[86320:8464807] D restkit.object_mapping:RKMappingOperation.m:1172 Starting mapping operation...
2016-01-18 09:50:31.263 CloudAPIRestKit[86320:8464807] T restkit.object_mapping:RKMappingOperation.m:1173 Performing mapping operation: <RKMappingOperation 0x7fa958696100> for 'CloudFileModel' object. Mapping values from object 
    checksum = xxx;
    contentToken = "xxx-xxx~";
    name = "2015-09-18 104310-4377094.JPG";
    parentPath = "/xxx";
    size = 4377094;
    systemAttributes =     
        "Capture-Date" = "2015-09-18T14:43:10.000Z";
        "Geo-Latitude" = "40.00000";
        "Geo-Longitude" = "-74.00000";
        Height = 3024;
        "Mime-Type" = "image/jpeg";
        Orientation = 6;
        "Timeline-Date" = "2015-09-18T14:43:10.000Z";
        Width = 4032;
    ;
    version = 1;
    versionCreated = "2015-09-18T14:53:36.525Z";
 to object <CloudFileModel: 0x7fa95a822d70> with object mapping (null)
2016-01-18 09:50:31.264 CloudAPIRestKit[86320:8464807] T restkit.object_mapping:RKMappingOperation.m:693 Found transformable value at keyPath 'parentPath'. Transforming from class '__NSCFString' to 'NSString'
2016-01-18 09:50:31.265 CloudAPIRestKit[86320:8464807] T restkit.object_mapping:RKMappingOperation.m:712 Mapping attribute value keyPath 'parentPath' to 'parentPath'
2016-01-18 09:50:31.265 CloudAPIRestKit[86320:8464807] T restkit.object_mapping:RKMappingOperation.m:728 Mapped attribute value from keyPath 'parentPath' to 'parentPath'. Value: /VZMOBILE/Cellco Test’s iPhone
2016-01-18 09:50:31.266 CloudAPIRestKit[86320:8464807] T restkit.object_mapping:RKMappingOperation.m:693 Found transformable value at keyPath 'name'. Transforming from class '__NSCFString' to 'NSString'
2016-01-18 09:50:31.266 CloudAPIRestKit[86320:8464807] T restkit.object_mapping:RKMappingOperation.m:712 Mapping attribute value keyPath 'name' to 'name'
2016-01-18 09:50:31.267 CloudAPIRestKit[86320:8464807] T restkit.object_mapping:RKMappingOperation.m:728 Mapped attribute value from keyPath 'name' to 'name'. Value: 2015-09-18 104310-4377094.JPG
2016-01-18 09:50:31.267 CloudAPIRestKit[86320:8464807] D restkit.object_mapping:RKMappingOperation.m:1273 Finished mapping operation successfully...
2016-01-18 09:50:31.268 CloudAPIRestKit[86320:8464807] D restkit.object_mapping:RKMapperOperation.m:258 Asked to map source object 
    checksum = xxx;
    contentToken = "xxx-duHZ0DyOA-g~";
    name = "2015-09-18 104307-3859795.JPG";
    parentPath = "/xxx";
    size = 3859795;
    systemAttributes =     
        "Capture-Date" = "2015-09-18T14:43:07.000Z";
        "Geo-Latitude" = "40.00000";
        "Geo-Longitude" = "-74.00000";
        Height = 3024;
        "Mime-Type" = "image/jpeg";
        Orientation = 6;
        "Timeline-Date" = "2015-09-18T14:43:07.000Z";
        Width = 4032;
    ;
    version = 1;
    versionCreated = "2015-09-18T14:53:19.610Z";
 with mapping <RKObjectMapping:0x7fa958654190 objectClass=CloudFileModel propertyMappings=(
    "<RKAttributeMapping: 0x7fa958729b60 parentPath => parentPath>",
    "<RKAttributeMapping: 0x7fa958654500 name => name>"
)>...
2016-01-18 09:50:33.803 CloudAPIRestKit[86320:8464807] D restkit.object_mapping:RKMappingOperation.m:1172 Starting mapping operation...

2016-01-18 09:50:33.901 CloudAPIRestKit[86320:8464807] D restkit.object_mapping:RKMappingOperation.m:1172 Starting mapping operation...
2016-01-18 09:50:33.902 CloudAPIRestKit[86320:8464807] T restkit.object_mapping:RKMappingOperation.m:1173 Performing mapping operation: <RKMappingOperation 0x7fa95ab51040> for 'CloudFileModel' object. Mapping values from object 
    checksum = xxx;
    contentToken = "xxx~";
    name = "2015-06-26 133729-1708809.JPG";
    parentPath = "/xxx";
    size = 1708809;
    systemAttributes =     
        "Capture-Date" = "2015-06-26T17:37:29.000Z";
        "Geo-Latitude" = "40.00000";
        "Geo-Longitude" = "-74.00000";
        Height = 2448;
        "Mime-Type" = "image/jpeg";
        Orientation = 6;
        "Timeline-Date" = "2015-06-26T17:37:29.000Z";
        Width = 3264;
    ;
    version = 4;
    versionCreated = "2015-09-16T19:51:37.749Z";
 to object <CloudFileModel: 0x7fa95aa212b0> with object mapping (null)

这是调试器在 [[RKObjectRequestOperation responseMappingQueue] addOperation:self.responseMapperOperation]; 之前显示的内容;

【问题讨论】:

显示 JSON。映射中只有 1 个键? @Wain 我拥有映射中的所有键,但我将其缩减为一个以简化操作。在问题中添加了 JSON sn-p。 我在那个 JSON 中看不到数据键,打开 RestKit 中的跟踪日志 更新的 JSON sn-p 看起来没有明显错误,需要跟踪日志 【参考方案1】:

也许,您还没有将描述符添加到对象管理器中:

[objectManager addResponseDescriptor:responseDescriptor];

【讨论】:

以上是关于Restkit 返回具有 0 个键值对的对象数组的主要内容,如果未能解决你的问题,请参考以下文章

java 怎么样获取Map中第几个键值对

从字典中提取键值对的子集?

如何为具有不同数量的键/值对的对象添加类型? [关闭]

对HashMap的理解

JS基础 Map是一组键值对的结构

Redis数据类型:HashesGeo操作指令