1
votes

There are plenty of examples of how to use RestKit with the integrated network stack, however, I simply want to take a JSON string, and map it to an object.

How do I do this with RestKit, current version (0.20.x) ?

1
@LithuT.V I'm not talking about good REST development practices. I'm talking about an iOS and OSX library that does object mapping, networking and persistence. . . the features seem tightly integrated, and I want to use the object mapping part on its own. - Jasper Blues
I didnt say anything about "good REST development".It was a workaround.I know what restkit is - Lithu T.V
@LithuT.V hehe - weird! When I clicked on your comment the first time, I was taken to page about REST on Android. - Jasper Blues

1 Answers

1
votes

I did find the answer in the documentation:

NSString* JSONString = @"{ \"name\": \"The name\", \"number\": 12345}";
NSString* MIMEType = @"application/json";
NSError* error;
NSData *data = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&error];
if (parsedData == nil && error) {
    // Parser error...
}

NSDictionary *mappingsDictionary = @{ @"someKeyPath": someMapping };
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
NSError *mappingError = nil;
BOOL isMapped = [mapper execute:&mappingError];
if (isMapped && !mappingError) {
    // Yay! Mapping finished successfully
}