7

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?

4 Answers 4

12
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

Sign up to request clarification or add additional context in comments.

5 Comments

I get No visible @interface for 'NSMutableArray' declares the selector 'addobject:'
Thanks it's working but my first question is still not working. promotion, product, content and subject are not being parsed in the JSON string
Can you please print the data in promotion, product, content and subject.
Sorry my mistake promotion was initialized as int - and therefore it couldn't add it to the dictionary. Unfortunately I didn't get warning or errors about it
most of the times try to use NSNumber
4
 NSError *error;
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"1", @"id",
                                @"test", @"toClientGroupType",
                                @"test", @"dueDate",
                                @"test", @"actionDate",
                                @"test", @"campaignType",
                                @"test", @"campaignCategory",
                                @"test", @"businessId",
                                @"test", @"promotion",
                                @"test", @"product",
                                @"test", @"content",
                                @"test", @"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);

Output :- [ { "subject" : "test", "toClientGroupType" : "test", "id" : "1", "dueDate" : "test", "actionDate" : "test", "campaignType" : "test", "businessId" : "test", "product" : "test", "content" : "test", "campaignCategory" : "test", "promotion" : "test" } ]

check out the data in to promotion, product, content and subject.it should not be nil or null

Comments

2
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

NSError *error;

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingAllowFragments error:&error];

 NSArray *categoryArray= [dic valueForKey:@"SELECTED OBJECT KEY"];
    NSLog(@"category%@",categoryArray);
}

category: Display Category array content

1 Comment

Get your Url for example (example.com) NSString *path = @" "; NSURL *urlpath = [NSURL URLWithString:path]; NSURLRequest *request = [NSURLRequest requestWithURL:urlpath cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:100.0 ]; NSURLConnection * connection = [[NSURLConnection alloc]initWithRequest:request delegate:self ]; [connection start]; and get data from above dictionary.
1

To problem A:

I think your fields are missing because they contain nil values. Keys that contain nil values are not considered when you use NSJSONSerialization

To problem B:

prashant has posted a good solution

4 Comments

I get No visible @interface for 'NSMutableArray' declares the selector 'addobject:' for the arr line
Regarding problem A the fields are not empty. I explicitly set them: for instance: NSString *product = @"EMAIL";
Hm, this is strange. Have you thought about using an external json library like JSONKit? I use it in many of my projects and never had any problems with it.
Ok, then I have no other idea than to check your vars again, cause your code seems to be right. Btw. you have not mentioned in you thread that promotion is missing in the output too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.