3

我在使用 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

这是City类(包含一个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'

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

4

1 回答 1

0

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

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

于 2012-11-20T15:56:08.470 回答