我正在使用 Restkit 0.20.x 从 json 映射对象,例如
{
"d":{
"results":[
{
"Web":[
"key1":"value1",
"key2":"value2"
],
"Image":[
"key1":"value1",
"key2":"value2"
],
},
],
},
}
我的主要目的是管理“Web”和“Image”键。我正在尝试映射对象但停留在“结果”键(键“结果”的值是一个只有一个元素作为字典的数组)。在我的情况下如何使用 RestKit 映射对象?
我的失败实现:
WFSD.h
@interface WFSD : NSObject
@property (nonatomic, strong) WFSResults *results;
@end
WFSResults.h
@interface WFSResults : NSObject
@property (nonatomic, strong) WFSResult *result;
@end
WFSResult.h
@interface WFSResult : NSObject
@property (nonatomic, strong) WFSWeb *web;
@property (nonatomic, strong) WFSImage *image;
@end
我的控制器.m
RKObjectMapping* dMapping = [RKObjectMapping mappingForClass:[WFSD class]];
RKObjectMapping* resultsMapping = [RKObjectMapping mappingForClass:[WFSResults class]];
RKRelationshipMapping* rsMapping1 = [RKRelationshipMapping relationshipMappingFromKeyPath:@"results" toKeyPath:@"results" withMapping:resultsMapping];
[dMapping addPropertyMapping:rsMapping1];
RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[WFSResult class]];
[resultsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"result" withMapping:resultMapping]];
RKRelationshipMapping* rsMapping2 = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Image" toKeyPath:@"Image" withMapping:imageMapping];
[resultMapping addPropertyMapping:rsMapping2];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dMapping
pathPattern:nil
keyPath:@"d"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];