1

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

Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
  • See this Question[http://stackoverflow.com/questions/13342253/is-there-a-way-we-can-give-dummy-input-to-restkit] and this answer[http://stackoverflow.com/questions/8788904/what-is-an-easy-way-to-stub-dummy-a-restful-web-service] – Lithu T.V Jun 19 '13 at 05:36
  • @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 Jun 19 '13 at 05:41
  • I didnt say anything about "good REST development".It was a workaround.I know what restkit is – Lithu T.V Jun 19 '13 at 05:48
  • @LithuT.V hehe - weird! When I clicked on your comment the first time, I was taken to page about REST on Android. – Jasper Blues Jun 19 '13 at 06:26

1 Answers1

1

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
}
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185