RestKit - load a simple array

I use RestKit in my iPhone application to download a list of countries. The problem is that the elementToPropertyMappings method uses a dictionary to map each object. In my case, I have an array of strings that I would like to map to the name property in my Country class.

Does anyone know how to do this?

elementToPropertyMappings

Should return a dictionary containing a mapping from the names of JSON elements to resource access devices

  • (NSDictionary *) elementToPropertyMappings Announced in RKObjectMappable.h

My JSON data

["Argentina","Australia","Austria","Belgium","Bolivia","Brazil","Bulgaria","Canada","Cayman Islands","China","Costa Rica","Croatia","Czech Republic","Denmark","Ecuador","Ethiopia","FYRO 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"] 

UPDATE:

I figured out how to use RKClient to query so that the mapping function is skipped. Now I need to figure out which class to use for parsing JSON. The yajl-objc parser looks great, but I don't want to include another parser if this can be done using lib from 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? } 
+6
source share
2 answers

Finding the right import for RKJSONParser was the hardest thing for me.

If there is another way to accomplish this with matching classes, please let me know.

Here is the code related to loading a simple array.

 #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]]; } 
+8
source

Array of string support has been added on v0.10: Source

0
source

Source: https://habr.com/ru/post/888614/


All Articles