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"属性をマップしてくださいurlimage.url

また、1つの画像ではなく、画像の配列全体が必要だと思うので、cityImages配列を次のようにマップします。 [cityMapping mapKeyPath:@"cityImages" toAttribute:@"cityImages"];

于 2012-11-20T15:56:08.470 に答える