Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create array of json objects in objective c

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?

like image 397
Dejell Avatar asked Sep 03 '12 10:09

Dejell


1 Answers

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

like image 177
prashant Avatar answered Nov 16 '22 21:11

prashant