RestKit不映射来自JSON的图像

时间:2013-05-10 16:59:39

标签: ios json restkit

无法正确映射我的JSON 响应中的图片,我无法弄清楚原因。

这是错误:“W restkit.object_mapping:RKObjectMappingOperation.m:244 keyPath'images'的值转换失败。没有策略从'__NSArrayM'转换为'Images'”

这是JSON:

{
    "timestamp": "2013-05-10T03:09:39Z",
    "feed": [{
        "headline": "Head text",
        "links": {
            "api": {
                "news": {
                    "href": "http://api.website.com/v1/91"
                },
                "self": {
                    "href": "http://api.website.com/v1/91"
                }
            },
            "web": {
                "href": "http://website.com/the/story"
            },
            "mobile": {
                "href": "http://m.website.com/wireless/story?storyId=9254113"
            }
        },
        "source": "Associated Press",
        "description": "Description text.",
        "images": [{
            "height": 324,
            "alt": "",
            "width": 576,
            "name": "Name text",
            "caption": "Caption text.",
            "url": "http://a.website.com/media/2013/0508.jpg"
        }],

Feed.h

@property (nonatomic, strong) Links *links;
@property (nonatomic, strong) Images *images;
@property (nonatomic, strong) Video *video;
@property (nonatomic, strong) NSString *headline;
@property (nonatomic, strong) NSString *source;
@property (nonatomic, strong) NSDate *published;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSString *premium;

+ (RKObjectMapping *) mapping;

Feed.m

+ (RKObjectMapping *)mapping {
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapKeyPathsToAttributes:
         @"headline", @"headline",
         @"source", @"source",
         @"published", @"published",
         @"description", @"description",
         @"premium", @"premium",
         nil];
        [mapping hasOne:@"links" withMapping:[Links mapping]];
        [mapping hasOne:@"images" withMapping:[Images mapping]];
        //[mapping hasMany:@"images" withMapping:[Images mapping]];
        [mapping hasOne:@"video" withMapping:[Video mapping]];
    }];

    return objectMapping;
}

Images.h

@property (nonatomic, strong) NSNumber *height;
@property (nonatomic, strong) NSNumber *width;
@property (nonatomic, strong) NSString *caption;
@property (nonatomic, strong) NSURL *url;

+ (RKObjectMapping *) mapping;

Images.m

+ (RKObjectMapping *)mapping {
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapKeyPathsToAttributes:
         @"height", @"height",
         @"width", @"width",
         @"caption", @"caption",
         @"url", @"url",
         nil];
    }];

    return objectMapping;
}

除图像外,其他所有内容都正确映射。我不知道它是否与它是一个数组但它只有一个结果,所以它不一定是一个数组?如果不是,那么我将如何编写...我只是不完全清楚它为什么不映射。

正如您所看到的,我尝试了hasMany:hasOne:映射并且都没有工作,我也遇到了同样的错误。

我尝试使用NSLog(@"url image: %@", feedLocal.images.url);' but get(null). Even though I thought it would work because NSLog(@“href web:%@”,feedLocal.links.web.href);`完美适用于我的链接href。

非常感谢任何帮助,非常感谢!

1 个答案:

答案 0 :(得分:1)

在Feed.h中,您需要将images属性更改为:

@property (nonatomic, strong) NSArray *images;

您应该将映射设置为hasMany: