0
我的應用程序需要提交一個JSON有效載荷到服務器時,服務器要像格式,以便:JSON格式和Objective-C
{
"recipient_emails" :["[email protected]", "[email protected]"]
}
所有我似乎可以創造是這樣的格式:
{
"recipient_emails" = ("[email protected]", "[email protected]");
}
我提交的內容不起作用,我無法判斷它是否是我們的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);
}
因此,當我將jsonData轉換回NSDictionary並打印出來(第二次打印時顯示的內容)時,爲方便起見添加了括號? – random
@random WYSNWYG(「你看到的不是你得到的」)。描述顯示的對象與JSON不同。打印出生成的JSON **字符串**,而您將看到它是正確的。 – 2013-06-21 19:45:04
我看,看起來像它的我們的STMP服務器。非常感謝您的幫助。 – random