在 Restkit 中映射单个 JSON 对象时出错

Posted

技术标签:

【中文标题】在 Restkit 中映射单个 JSON 对象时出错【英文标题】:Error mapping single JSON object in Restkit 【发布时间】:2014-06-15 21:47:18 【问题描述】:

我在将 JSON 响应映射到 Objective-C 对象时遇到问题。

这是响应的内容:


"pin": 
"title": "Donkey Kong Tower",
"description": "This is an article with #suchhashtag because it's awesome!",
"_id": "538054c107bf5b0b00795b82",
"user": "5380cc64db86cc0b002cdc63",
"updatedAt": "2014-06-15T21:34:41.891Z",
"createdAt": "2014-06-15T21:34:41.891Z",
"coordinates": 
  "latitude": 48.1678645,
  "longitude": 11.5861475
,
"id": "538054c107bf5b0b00795b82"


对应的对象头是这样的:

@interface Pin : NSObject

@property (strong, nonatomic) NSString *identifier;
@property (strong, nonatomic) NSString *description;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *latitude;
@property (strong, nonatomic) NSString *longitude;
@property (strong, nonatomic) NSString *userId;
@property (strong, nonatomic) NSString *pictureId;
@property (strong, nonatomic) NSString *recordingId;

@end

最后是映射设置:

RKObjectMapping* pinMapping = [RKObjectMapping mappingForClass:[Pin class]];

[pinMapping addAttributeMappingsFromDictionary:@@"coordinates.latitude": @"latitude",
                                            @"coordinates.longitude": @"longitude", 
                                            @"id": @"identifier", 
                                            @"user" : @"userId", 
                                            @"title" : @"title",
                                            @"pictureId": @"pictureId", 
                                            @"recordingId": @"recordingId", 
                                            @"description" : @"description"];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://server.herokuapp.com/pins/%@?access_token=%@", identifier, accessToken]]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor     
                                        responseDescriptorWithMapping:pinMapping 
                                        method:RKRequestMethodAny 
                                        pathPattern:nil 
                                        keyPath:@"pin" 
                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request 
                                        responseDescriptors:@[responseDescriptor]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 

 failure:^(RKObjectRequestOperation *operation, NSError *error) 
  RKLogError(@"Operation failed with error: %@", error);
];
[objectRequestOperation start];

日志文件表明映射确实有效,但生成的对象仅包含描述字段的内容。

restkit.object_mapping:RKMapperOperation.m:229 Asked to map source object 
    "_id" = 538054c107bf5b0b00795b82;
    coordinates =     
        latitude = "48.1678645";
        longitude = "11.5861475";
    ;
    createdAt = "2014-06-15T21:36:08.412Z";
    description = "This is an article with #suchhashtag because it's awesome!";
    id = 538054c107bf5b0b00795b82;
    title = "Donkey Kong Tower";
    updatedAt = "2014-06-15T21:36:08.412Z";
    user = 5380cc64db86cc0b002cdc63;
 with mapping <RKObjectMapping:0x10b351d60 objectClass=Pin propertyMappings=(
    "<RKAttributeMapping: 0x10b35b170 _id => identifier>",
    "<RKAttributeMapping: 0x10b3555d0 coordinates.latitude => latitude>",
    "<RKAttributeMapping: 0x10b355020 pictureId => pictureId>",
    "<RKAttributeMapping: 0x10b355370 recordingId => recordingId>",
    "<RKAttributeMapping: 0x10b330cd0 title => title>",
    "<RKAttributeMapping: 0x10b356ee0 coordinates.longitude => longitude>",
    "<RKAttributeMapping: 0x10b357400 description => description>",
    "<RKAttributeMapping: 0x10b356d10 user => userId>"
)>
2014-06-15 23:36:08.454 Remarkable[7523:f03] D restkit.object_mapping:RKMappingOperation.m:859 Starting mapping operation...
2014-06-15 23:36:08.454 Remarkable[7523:f03] T restkit.object_mapping:RKMappingOperation.m:860 Performing mapping operation: <RKMappingOperation 0x10b3646f0> for 'Pin' object. Mapping values from object 
    "_id" = 538054c107bf5b0b00795b82;
    coordinates =     
        latitude = "48.1678645";
        longitude = "11.5861475";
    ;
    createdAt = "2014-06-15T21:36:08.412Z";
    description = "This is an article with #suchhashtag because it's awesome!";
    id = 538054c107bf5b0b00795b82;
    title = "Donkey Kong Tower";
    updatedAt = "2014-06-15T21:36:08.412Z";
    user = 5380cc64db86cc0b002cdc63;
 to object *nil description* with object mapping (null)
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath '_id'. Transforming from class '__NSCFString' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath '_id' to 'identifier'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath '_id' to 'identifier'. Value: 538054c107bf5b0b00795b82
restkit.object_mapping:RKMappingOperation.m:518 Did not find mappable attribute value keyPath 'pictureId'
restkit.object_mapping:RKMappingOperation.m:518 Did not find mappable attribute value keyPath 'recordingId'
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'title'. Transforming from class '__NSCFString' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'title' to 'title'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'title' to 'title'. Value: Donkey Kong Tower
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'description'. Transforming from class '__NSCFString' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'description' to 'description'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'description' to 'description'. Value: This is an article with #suchhashtag because it's awesome!
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'user'. Transforming from class '__NSCFString' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'user' to 'userId'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'user' to 'userId'. Value: 5380cc64db86cc0b002cdc63
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'coordinates.latitude'. Transforming from class '__NSCFNumber' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'coordinates.latitude' to 'latitude'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'coordinates.latitude' to 'latitude'. Value: 48.1678645
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'coordinates.longitude'. Transforming from class '__NSCFNumber' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'coordinates.longitude' to 'longitude'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'coordinates.longitude' to 'longitude'. Value: 11.5861475
restkit.object_mapping:RKMappingOperation.m:928 Finished mapping operation successfully...
restkit.object_mapping:RKMapperOperation.m:403 Finished performing object mapping. Results: 
    pin = "This is an article with #suchhashtag because it's awesome!";
    

(我省略了时间戳) 我认为主要错误在于

to object *nil description* with object mapping (null)

部分。 另一个 *** 问题也有类似的问题:RestKit 0.2 result is an array of nil objects, although mapping seems to be succesful,虽然没有明确的解决方案。

【问题讨论】:

【参考方案1】:

因为description 是用于返回实例描述的预定义方法 - 不要通过添加您自己的同名属性来覆盖它。

将您的属性名称更改为“overview”或类似名称并更新映射目标键。

【讨论】:

以上是关于在 Restkit 中映射单个 JSON 对象时出错的主要内容,如果未能解决你的问题,请参考以下文章

RestKit:将单个对象映射到现有数组

使用 RestKIt 在核心数据实体中映射 json 对象

手动映射 JSON 对象时,RestKit 关系未映射

如何在 Objective C 中使用 RestKit 将纬度和经度字段映射到单个 CLLocationCoordinate2D?

在其 HTTP 正文中嵌套 JSON 的 RestKit 对象映射请求

Restkit 0.20 对象在获取 json 响应后未映射