Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ObjC RestKit library how to map object to JSON NSString *

Tags:

json

ios

restkit

I'm using RestKit http://restkit.org for iOS. I have an object and an object mapping defined and can use that to send and receive data with the server. However for my mapped objects I'd like to the -description method to return the JSON mapping for logging to the console.

How to map the object to a string?

like image 281
bradgonesurfing Avatar asked Mar 19 '26 19:03

bradgonesurfing


1 Answers

The object mapping has now changed substantially as of RestKit ObjectMapping 2.0 in the newer versions of Reskit, and @bradgonesurfing's answer won't work in these newer versions.

You now need to use RKObjectParameterization to perform the object serialization as follows:

RKObjectMapping *itemMapping = [RKObjectMaping mappingForClass:[Item class]];
/* Your object mapping definition for the Item class goes here */

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:itemMapping.inverseMapping objectClass:[Item class] rootKeyPath:nil method:RKRequestMethodPOST];

NSDictionary *dict = [RKObjectParameterization parametersWithObject:item requestDescriptor:requestDescriptor error:nil];
NSData *jsonData = [RKMIMETypeSerialization dataFromObject:dict MIMEType:RKMIMETypeJSON error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
like image 194
Jonathan Ellis Avatar answered Mar 21 '26 10:03

Jonathan Ellis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!