RestKit对象映射 - 解析嵌套数组

时间:2012-03-08 07:47:42

标签: ios restkit

我非常依赖于映射一个简单的JSON对象:

{"articles":
[{"id":"354633","lat":"51.501","lng":"-0.125","type":"railwaystation","title":"Westminster tube station","url":"http:\/\/en.wikipedia.org\/w\/index.php?curid=354633","distance":"53m"},

{"id":"92601","lat":"51.5011","lng":"-0.125","type":"railwaystation","title":"17  - Westminster tube station","url":"http:\/\/en.wikipedia.org\/w\/index.php?   curid=92601","distance":"62m"},    

{"id":"92598","lat":"51.5011","lng":"-0.125","type":"railwaystation","title":"34 -Westminster tube station","url":"http:\/\/en.wikipedia.org\/w\/index.php?curid=92598","distance":"62m"}]
}

我对我遇到的问题感到很困惑,因为这几乎与RestKit Mapping Guide中的JSON相同。

我的代码是:

self.articleMapper = [RKObjectMapping mappingForClass:[WikiArticle class]];
[self.articleMapper mapKeyPath:@"title" toAttribute:@"title"];    
[[RKObjectManager sharedManager].mappingProvider setMapping:self.articleMapper forKeyPath:@"articles"];

我在日志中收到以下错误:

restkit.object_mapping:RKObjectMappingOperation.m:339 Did not find mappable attribute value keyPath 'title'
restkit.object_mapping:RKObjectMappingOperation.m:557 Mapping operation did not find any mappable content
restkit.object_mapping:RKObjectMapper.m:308 The following operations are in the queue: ()
restkit.object_mapping:RKObjectMapper.m:323 Finished performing object mapping. Results: {}

先谢谢你帮我解决了这么简单的问题 - 我几个小时以来一直在抨击它!

-Alex

1 个答案:

答案 0 :(得分:0)

我想你用这样的方式来称呼它:

[objectManager loadObjectsAtResourcePath:url delegate:self block:^(RKObjectLoader* loader) {
        loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[WikiArticle class]];
    }];

更好的尝试:

[objectManager loadObjectsAtResourcePath:url delegate:self];