Jasper Blues
Jasper Blues

Reputation: 28756

RestKit: How to map an arbitrary string

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) ?

Upvotes: 1

Views: 256

Answers (1)

Jasper Blues
Jasper Blues

Reputation: 28756

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
}

Upvotes: 1

Related Questions