I follow this link https://github.com/RestKit/RestKit/wiki/Posting-NSDictionary-as-JSON to create the json post, and get back json response from server. How could I continue process the json response to a list of objects?
- (void)sendAsJSON:(NSDictionary*)dictionary {
RKClient *client = [RKClient clientWithBaseURL:@"http://restkit.org"];
// create a JSON string from your NSDictionary
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
NSError *error = nil;
NSString *json = [parser stringFromObject:dictionary error:&error];
// send your data
if (!error)
[[RKClient sharedClient] post:@"/some/path" params:[RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON] delegate:self];
}
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response
{
NSLog(@"after posting to server, %@", [response bodyAsString]);
}
EDIT1: this is the Json I want to POST to server.
{
"memberId": "1000000",
"countryCode": "US",
"contacts": [
{
"phoneNumber": "+12233333333",
"memberId": "2222",
"contactId": "123456",
"name": "john"
},
{
"phoneNumber": "+12244444444",
"memberId": "3333",
"contactId": "123457",
"name": "mary"
}
]
}
EDIT2: Someone actually resolved this in another thread.
https://stackoverflow.com/a/7726829/772481
Use RKObjectManger instead of RKClient to do the POST. You can then load the response into objects when this method is called:
EDIT (given JSON to send to server):
Instead of creating the JSON the way that you’re currently doing it, you can create custom model classes.
First, you can create a model class for your top level object (assuming it’s called User).
User Header
User Implementation
Then, you can create a model class called Contact.
Contact Header
Contact Implementation
You can use these classes as so:
I would need to know what kind of JSON response you’re expecting before I can show you how to do the serialization mapping and the POST.
EDIT (given JSON returned from server):
To set the serialization mapping and object mapping and POST it, you’ll need to set up the resource path (I do it when I launch my app):
Your resource path may be something other than “/users”.
Take a look at the code under the comment
//Then you set up a serialization mapping and object mapping and POST it, where I have added the serialization mapping, object mapping, and POSTing.