我在我的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发出请求,以便跳过Mapping功能。现在我需要弄清楚用于JSON解析的类。 yajl-objc解析器看起来很棒,但如果可以使用RestKit中的lib完成,我不想包含另一个解析器。
-(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?
}
答案 0 :(得分:8)
找出RKJSONParser的正确导入对我来说是最具挑战性的事情。
如果有其他方法可以使用Mapping类完成此操作,请告诉我。
以下是加载简单数组所涉及的代码。
#import <RestKit/Support/RKJSONParser.h>
@implementation CountriesViewController
@synthesize countries;
-(void)loadLocations
{
NSLog(@"loadLocations");
[[RKClient sharedClient] get:@"/locations/countries.json" delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
NSLog(@"Loaded payload: %@", [response bodyAsString]);
RKJSONParser* parser = [RKJSONParser new];
countries = [parser objectFromString:[response bodyAsString]];
}
答案 1 :(得分:0)
在v0.10:Source
上添加了对字符串数组的支持