我正在尝试将中等复杂的JSON映射到嵌套的对象结构中。 Shops对象是递归的,但ErrorStatus对象不是。但是,在reationshipMappingFromKeyPath:@“ErrorStatus”之后的调试断点显示了entityMapping - > ErrorStatus通过_mutablePropertyMappings进行深度递归。不幸的是调试似乎不支持复制,否则我会在这里显示。任何提示都会受到高度赞赏。
@implementation LLSResponse
+ (RKObjectMapping *)jsonMapping
{
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace); //??? debugging
RKLogConfigureByName("RestKit/Networking", RKLogLevelTrace); //??? debugging
NSLog(@"===== LLSResponse.jsonMapping/RestKitMapping");
RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[LLSResponse class]];
[entityMapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:@"ErrorStatus"
toKeyPath:@"ErrorStatus"
withMapping:[ErrorStatus jsonMapping]]];
[entityMapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:@"Shops"
toKeyPath:@"Shops"
withMapping:[Shops jsonMapping]]];
[entityMapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:@"Events"
toKeyPath:@"Events"
withMapping:[Events jsonMapping]]];
return entityMapping;
}
- (NSArray *) getAllEvents
{
NSLog(@"============ getAllEvents");
Events * events = self.events;
NSArray *allEvents = events.events;
return allEvents;
}
@end
@implementation ErrorStatus
+ (RKObjectMapping *)jsonMapping
{
NSLog(@"===== ErrorStatus.jsonMapping");
RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[ErrorStatus class]];
[entityMapping addAttributeMappingsFromDictionary:@{
@"@ID" : @"_id",
@"@Status" : @"Status",
}];
return entityMapping;
}
@end
@implementation Events
+ (RKObjectMapping *)jsonMapping
{
NSLog(@"===== Events.jsonMapping");
RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[Events class]];
[entityMapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:@"Event"
toKeyPath:@"Event"
withMapping:[EventDO jsonMapping]]];
return entityMapping;
}
@end
@implementation Shop
+ (RKObjectMapping *)jsonMapping
{
NSLog(@"===== Shop.jsonMapping");
RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[Shop class]];
[entityMapping addAttributeMappingsFromDictionary:@{
@"@ID" : @"_id",
@"@Title" : @"title",
}];
[entityMapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:@"Shop"
toKeyPath:@"Shop"
withMapping:entityMapping]];
return entityMapping;
}
@end
@implementation Shops
+ (RKObjectMapping *)jsonMapping
{
NSLog(@"===== Shops.jsonMapping");
RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[Shops class]];
[entityMapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:@"Shop"
toKeyPath:@"Shop"
withMapping:[Shop jsonMapping]]];
return entityMapping;
}
@end
我发布了一个更常见的问题,包含更多代码和有问题的JSON,但这可能会捕捉问题的本质。见RestKit not mapping recursive JSON to objects