Create an array of json objects in a c object

I am new to objective-c and I need to send a 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. JsonData result as String -

 { "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 can see, I am missing 3 fields that I declared: content , product and subject

B. I really need to represent an array of objects, so the query would 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 this and what is wrong?

+6
source share
4 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

+11
source
  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 data in advertising, product, content and subject.it should not be nil or null

+3
source

To problem A:

I think your fields are missing because they contain nil values. Keys that contain nil values ​​are not taken into account when using NSJSONSerialization

To task B:

prashant posted a good solution

+1
source
 -(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: The contents of an array of category categories

+1
source

Source: https://habr.com/ru/post/924432/


All Articles