我使用Rest Kit来解析服务器中的数据。 我从服务器获得了这个JSON:
{
"title":""
"data":
{
"container":
[
{
"container_award":
[[[{"title":"","image":""}]]]
}
],...
}
}
我需要创建映射才能执行此操作。
我的问题在于为container_award
创建映射,因为我不知道如何在Rest Kit的映射中获取嵌套数组。
有我的映射,但不起作用:
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Card class]];
[responseMapping addAttributeMappingsFromDictionary:[Card getMapping]];
// if we have keyPath we also cold have subKeyPath
RKObjectMapping *cardContainerArrayMapping = [RKObjectMapping mappingForClass:[CardContainerArray class]];
RKObjectMapping *cardContainerMapping = [RKObjectMapping mappingForClass:[CardContainer class]];
[cardContainerMapping addAttributeMappingsFromDictionary:[CardContainer getMapping]];
RKObjectMapping *containerDataMapping = [RKObjectMapping mappingForClass:[CardContainerData class]];
[containerDataMapping addAttributeMappingsFromDictionary:[CardContainerData getMapping]];
// for three array
RKObjectMapping *containerDataAwardsMapping = [RKObjectMapping mappingForClass:[CardContainerData class]];
[containerDataAwardsMapping addAttributeMappingsFromDictionary:[CardContainerData getMapping]];
RKObjectMapping *array1Mapping = [RKObjectMapping mappingForClass:[NSArray class]];
[array1Mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil
toKeyPath:nil
withMapping:containerDataAwardsMapping]];
RKObjectMapping *array2Mapping = [RKObjectMapping mappingForClass:[NSArray class]];
[array2Mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil
toKeyPath:nil
withMapping:array1Mapping]];
[cardContainerMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"container_awards"
toKeyPath:@"container_awards"
withMapping:array2Mapping]];
[cardContainerArrayMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"containers"
toKeyPath:@"containers"
withMapping:cardContainerMapping]];
[responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"data"
toKeyPath:@"data"
withMapping:cardContainerArrayMapping]];
编辑: 卡类
@class CardContainerArray;
@interface Card : NSObject
@property(nonatomic,strong) NSString *title;
@property(nonatomic,strong) CardContainerArray *data;
CardContainerArray类:
@interface CardContainerArray : NSObject
@property(nonatomic,strong) NSArray *container;
@end
CardContainer类: @interface CardContainer:NSObject
@property(nonatomic,strong) NSArray* container_award;
CardContainerData:
@interface CardContainerData : NSObject
@property(nonatomic,strong) NSString *title;
@property(nonatomic,strong) NSString *image;
答案 0 :(得分:0)
你需要一个容器'用于处理JSON中container
的类,因为您无法作为映射的一部分索引到数组中。这个'容器'可以是一个自定义类,也可以是一个简单的字典,它取决于你想要的最终数据。
您也可以将container
映射到现有类的数组中,然后自己处理字典数组。
[[[{"title":"","image":""}]]]
非常垃圾,真的应该被清理掉,因为三重嵌套数组似乎不合逻辑......
在编辑中,您并不想真正映射到NSArray
。最好它应该是NSMutableArray
,但你没有映射数组,你映射数组的内容。
处理[[[{"title":"","image":""}]]]
:
可能最简单的选择就是作弊...你已经有@property(nonatomic,strong) NSString *title;
和@property(nonatomic,strong) NSString *image;
,但不要在映射中使用它。相反,简单地使映射imageMessyArray
并使用:
- (NSArray *)imageMessyArray { return @[]; }
- (void)setImageMessyArray:(NSArray *)messyArray {
NSDictionary *imageDict = [[[messyArray firstObject] firstObject] firstObject];
self.title = [imageDict objectForKey:@"title"];
self.image = [imageDict objectForKey:@"image"];
}
它真的很乱。
正确的方法是创建一个到CardContainerData
变换器的数组,它基本上会包装相同的代码,但会更干净地封装它。