I am new to objective-c and need to submit collection of json objects.
I wrote the following:
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
id, @"id",
toClientGroupType, @"toClientGroupType",
dueDate, @"dueDate",
actionDate, @"actionDate",
campaignType, @"campaignType",
campaignCategory, @"campaignCategory",
businessId, @"businessId",
promotion, @"promotion",
product, @"product",
contentF, @"content",
subject, @"subject",
nil];
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);
[request setURL:[NSURL URLWithString:@"https://services-dev.a.com/api/channels"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData2];
I have 2 problems:
A. The output of jsonData as String is
{
"toClientGroupType" : "VIP",
"id" : "1",
"dueDate" : "2012-09-03 10:25:42 +0000",
"actionDate" : "2012-09-03 10:25:42 +0000",
"campaignType" : "ONE_TIME",
"businessId" : "150",
"campaignCategory" : "SALE"
}
As you see - I am missing 3 fiels which I declared: content
, product
and subject
B. I actually need to submit an array of objects so the request will be like this:
[{
"toClientGroupType" : "VIP",
"id" : "1",
"dueDate" : "2012-09-03 10:25:42 +0000",
"actionDate" : "2012-09-03 10:25:42 +0000",
"campaignType" : "ONE_TIME",
"businessId" : "150",
"campaignCategory" : "SALE"
}]
How can I do it and what is wrong?
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
id, @"id",
toClientGroupType, @"toClientGroupType",
dueDate, @"dueDate",
actionDate, @"actionDate",
campaignType, @"campaignType",
campaignCategory, @"campaignCategory",
businessId, @"businessId",
promotion, @"promotion",
product, @"product",
contentF, @"content",
subject, @"subject",
nil];
NSMutableArray * arr = [[NSMutableArray alloc] init];
[arr addObject:jsonDictionary];
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);
Checkout this one
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With