加载一个简单的数组

我在我的iPhone应用程序中使用RestKit来加载国家列表。 问题是elementToPropertyMappings方法使用字典映射每个对象。 在我的情况下,我有一个字符串数组,我想映射到Country 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?
    }
    

    搞清楚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]]; 
    }
    

    对v0.10:Source添加了对字符串数组的支持

    链接地址: http://www.djcxy.com/p/49187.html

    上一篇: Load a simple array

    下一篇: gcc and g++ library search paths