使用Restkit和错误映射JSON数组:没有从'__NSArrayI'转换为'NSString'的策略

时间:2012-11-20 08:35:30

标签: json mapping restkit

我在使用RestKit映射JSON数组时遇到了一些麻烦。

这是JSON文件:

 "result": {"cities": [
                {"city": {"id": 2, "name": "Madrid"}, 
                 "cityImages": [{"image": {"url": "", 
                                           "image": "", 
                                           "blob": "", 
                                           "title":"Madrid"}
                                }]
                  }, 

                  {"city": {"id": 11001, "name": "Seville"}, 
                   "cityImages": [{"image": {"url": "", 
                                             "image": "", 
                                              "blob": "", 
                                              "title": "Seville Foto"}}, 
                                  {"image": {"url": "", 
                                             "image": "", 
                                             "blob": "", 
                                             "title": "Otra"}
                                   }]
                    }]
            }

问题在于CityImage数组。 这是我的班级CityImage

@interface CityImage : NSObject 
@property (nonatomic, strong) NSString *imageURL;
@property (nonatomic, strong) NSString *image; 
@property (nonatomic, strong) NSString *imageBase64; 
@property (nonatomic, strong) NSString *title; 
@end

这是班级城市(包含一系列CityImages)

@interface City : NSObject
@property (nonatomic, strong) NSString *cityid;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *cityImages;

现在,这是我对cityImages的映射:

RKObjectMapping* cityImageMapping = [RKObjectMapping mappingForClass:[CityImage class] ];
[cityImageMapping mapKeyPath:@"image.url" toAttribute:@"imageURL"];
[cityImageMapping mapKeyPath:@"image.image" toAttribute:@"image"];
[cityImageMapping mapKeyPath:@"image.blob" toAttribute:@"imageBase64"];
[objectManager.mappingProvider setMapping:cityImageMapping forKeyPath:@"result.cities.cityImages"];

对于城市:

RKObjectMapping *cityMapping = [RKObjectMapping mappingForClass:[City class]];
[cityMapping mapKeyPathsToAttributes:@"city.id", @"cityid", @"city.name", @"name", nil];
[cityMapping mapKeyPath:@"cityImages.image" toAttribute:@"cityImages"];
[objectManager.mappingProvider setMapping:cityMapping forKeyPath:@"result.cities"];

当解析JSON时,它会给我这个错误:

W restkit.object_mapping:RKObjectMappingOperation.m:244 Failed transformation of value at keyPath 'image.url'. No strategy for transforming from '__NSArrayI' to 'NSString'
W restkit.object_mapping:RKObjectMappingOperation.m:244 Failed transformation of value at keyPath 'image.blob'. No strategy for transforming from '__NSArrayI' to 'NSString'
W restkit.object_mapping:RKObjectMappingOperation.m:244 Failed transformation of value at keyPath 'image.url'. No strategy for transforming from '__NSArrayI' to 'NSString'
W restkit.object_mapping:RKObjectMappingOperation.m:244 Failed transformation of value at keyPath 'image.blob'. No strategy for transforming from '__NSArrayI' to 'NSString'

我想我已经忘记了什么,但是虽然我已经尝试了几乎所有我发现的东西,但问题仍然存在。 任何的想法?。 谢谢你的帮助!!

1 个答案:

答案 0 :(得分:0)

尝试为cityImageMapping设置此项:cityImageMapping.rootKeyPath = @"image"并将您的属性映射为url而不是image.url

另外,我相信您需要整个图像阵列而不是一个图像,因此映射cityImages数组如下: [cityMapping mapKeyPath:@"cityImages" toAttribute:@"cityImages"];