0

我的应用程序需要向服务器提交 JSON 有效负载,服务器需要如下格式:

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

我似乎只能创建这种格式:

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

我提交的内容不起作用,我不知道这是我们的 stmp 邮件服务器的服务器问题还是我提交的 JSON 格式的问题。

这是我创建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);
    }
4

1 回答 1

1

使用括号的描述NSArray- 不用担心,如果使用NSJSONSerialization,生成的 JSON 将正确包含方括号。

于 2013-06-21T19:41:05.960 回答