2012-03-08 176 views
0

我在映射簡單JSON對象極其卡住:RestKit對象映射 - 解析嵌套數組

{"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"}] 
} 

我真的由我有問題困惑,因爲這是幾乎相同的JSON在RestKit映射指南。

我的代碼是:

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

回答

0

我想你把它的方式是這樣的:

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

最好嘗試:

[objectManager loadObjectsAtResourcePath:url delegate:self];