Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RestKit - Load data from local json file
NSString *filePath = [[NSBundle mainBundle] pathForResource:JohnSmith ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
if (data) {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
KCUser *appUser = [[KCUser alloc] init];
NSString* MIMEType = @"application/json";
NSError* error;
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&error];
if (parsedData == nil && error) {
NSLog(@"parser error");
}
NSDictionary *mappingsDictionary = @{ @"appUser": [KCUser mapping] };
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
mapper.targetObject = appUser;
NSError *mappingError = nil;
BOOL isMapped = [mapper execute:&mappingError];
if (isMapped && !mappingError) {
[[KCUserSingleton sharedKCUserSingleton] setUser:appUser];
}
else {
NSLog(@"error desc: %@", error.localizedDescription);
NSLog(@"error reason: %@", error.localizedFailureReason);
NSLog(@"error suggestion: %@", error.localizedRecoverySuggestion);
}
}
@kyleclegg

This comment has been minimized.

Copy link
Owner Author

kyleclegg commented Jun 23, 2013

Shows how to load a json file locally. Assumes all mappings have been configured correctly.

@woodmister1

This comment has been minimized.

Copy link

woodmister1 commented Oct 8, 2013

on line 15, [KCUser mapping], how are you adding any relationships between user and for example sub RKObjectMapper types, as this is returning a dictionary, or can the dictionary contain @"key", [KCUser2 mapping]...

@kkotar

This comment has been minimized.

Copy link

kkotar commented Nov 28, 2013

woodminister1 try this:

Mappings Dictionary

The mappings dictionary describes how to object map the source object. The keys of the dictionary are key paths into the representation and the values are RKMapping objects describing how to map the representations at the corresponding key path. This dictionary based approach enables a single document to contain an arbitrary number of object representations that can be mapped independently. Consider the following example JSON structure:

{ "tags": [ "hacking", "phreaking" ], "authors": [ "Captain Crunch", "Emmanuel Goldstein" ], "magazine": { "title": "2600 The Hacker Quarterly" } }
Each key in the document could be mapped independently by providing a mapping for the key paths:

RKObjectMapping *tagMapping = [RKObjectMapping mappingForClass:[Tag class]];
RKObjectMapping *authorMapping = [RKObjectMapping mappingForClass:[Author class]];
RKObjectMapping *magazineMapping = [RKObjectMapping mappingForClass:[Magazine class]];
NSDictionary *mappingsDictionary = @{ @"tag": tagMapping, @"author": authorMapping, @"magazine": magazine };

more info : http://restkit.org/api/latest/Classes/RKMapperOperation.html

or you can use response descriptors from RKObjectManager:

NSMutableDictionary *mappingsDictionary = [[NSMutableDictionary alloc] init];
for (RKResponseDescriptor *descriptor in _objectManager.responseDescriptors) {
[mappingsDictionary setObject:descriptor.mapping forKey:descriptor.keyPath];
}

@kyleclegg

This comment has been minimized.

Copy link
Owner Author

kyleclegg commented Dec 12, 2013

@woodmister1 - I like to setup my RestKit mappings within each model class rather than all at the time of configuration, which could get massive for a larger project. Then, I create a public class method to return the mapping for that class. Does that make sense? That is what I am calling on line 15. I can show my model class as well if you'd like.

@tolentinokas

This comment has been minimized.

Copy link

tolentinokas commented Feb 21, 2017

What is the targetObject for?

@slxl

This comment has been minimized.

Copy link

slxl commented Jan 28, 2018

thanks for the snippet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.