0

My app needs to submit a JSON payload to a server, the server wants the format like so:

{
"recipient_emails" :["test@xyz.net", "user@xyz.com"]
}

All I can seem to create is this format:

{
    "recipient_emails" = ("test@xyz.net", "user@xyz.com");
}

What I'm submitting doesn't work, which I can't tell if it's a server issue with our stmp mail server or the format of the JSON i'm submitting.

Here is how I create the json:

    //Grab the data (from NSManagedObject)
    NSString *emailData = [[NSString alloc] initWithData:self.submissionRecipients encoding:NSUTF8StringEncoding];

    //Extract all the separate email address
    NSArray *emails = [emailData componentsSeparatedByString:@","];

    //If there are some there (which should always be)
    if (emails) {
        NSError *error;

        //Add the array to an NSDictionary with key and convert to JSON
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSDictionary dictionaryWithObject:emails forKey:@"recipient_emails"] options:0 error:&error];

        //Add json to HTTPRequest
        [theRequest setHTTPBody:jsonData];

        //Print out for debug purposes
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
        NSLog(@"%@", dic);
    }
random
  • 8,568
  • 12
  • 50
  • 85

1 Answers1

1

The description of NSArray uses parenthesis - don't worry, if you use NSJSONSerialization, the generated JSON will properly contain square brackets.

  • So when I convert jsonData back to a NSDictionary and print it out (what's shown in the second print out), the parenthesis are added for convenience? – random Jun 21 '13 at 19:44
  • @random WYSNWYG ("what you see is NOT what you get"). The description shows the object differently than the JSON. Print out the generated JSON **string** instead and you'll see it's correct. –  Jun 21 '13 at 19:45
  • I see, looks like its our stmp server. Thank you very much for your help. – random Jun 21 '13 at 19:51