我在我的 iPhone 应用程序中使用RestKit来加载国家列表。问题是 elementToPropertyMappings 方法使用字典来映射每个对象。就我而言,我有一个字符串数组,我想将它们映射到我的 Country 类的 name 属性。
有人知道怎么做吗?
elementToPropertyMappings
必须返回包含从 JSON 元素名称到属性访问器的映射的字典
- (NSDictionary *)elementToPropertyMappings 在 RKObjectMappable.h 中声明
我的 JSON 数据
["Argentina","Australia","Austria","Belgium","Bolivia","Brazil","Bulgaria","Canada","Cayman Islands","China","Costa Rica","Croatia","Czech Republic","Denmark","Ecuador","Ethiopia","F.Y.R.O. Macedonia","Finland","France","French Polynesia","Germany","Guam","Hong Kong SAR","Indonesia","Ireland","Israel","Italy","Japan","Latvia","Lithuania","Luxembourg","Malaysia","Malta","Mexico","Morocco","Netherlands","New Zealand","Nicaragua","Norway","Papua New Guinea","Peru","Poland","Portugal","Puerto Rico","Qatar","Romania","Russia","Singapore","Slovakia","Slovenia","South Africa","South Korea","Spain","Sweden","Switzerland","Taiwan","United Arab Emirates","United Kingdom","United States","Venezuela","Vietnam"]
更新:
我想出了如何使用 RKClient 发出请求,以便跳过映射功能。现在我需要弄清楚使用什么类来解析 JSON。yajl-objc 解析器看起来很棒,但如果可以使用来自 RestKit 的库来完成,我不想包含另一个解析器。
-(void)loadLocations
{
NSLog(@"loadLocations");
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[[RKClient sharedClient] get:@"/locations/countries.json" delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
NSLog(@"Loaded payload: %@", [response bodyAsString]);
// HOW CAN I PARSE THIS STRING INTO AN NSArray?
}