2017-02-17 85 views
2

我想構建我的NSMutableDictionary發送到使用AFNetworking框架的請求,但似乎我很困惑如何正確地做到這一點。AFNetworking - NSMutableDictionary參數

這裏是服務器的期待

{ 
"do":"timeline", 
"what":"posting", 
"session":"", 
"data":{ 
"status_timeline":"", 
    "with_timeline":"", 
    "location_timeline":"", 
    "category_timeline":"", 
    "privacy_timeline":"" 
} 

,我已經試過這樣

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init]; 
    [dict setValue:@"timeline" forKey:@"do"]; 
    [dict setValue:@"posting" forKey:@"what"]; 
    [dict setValue:session forKey:@"session"]; 
    NSLog(@"Session %@", [dict valueForKey:@"session"]); 

我希望有人能幫助我感謝的

+0

歡迎來到SO。這是你的第二個問題,「我該怎麼做xyz」。這不是教程網站,您需要指定您的問題,到目前爲止所嘗試的內容,獲得的結果,遇到的錯誤以及期望的內容,以及針對您需要的特定問題的特定問題解決。請在通過http://stackoverflow.com/help/how-to-ask閱讀更多問題以避免出現標誌。有多個線程與您試圖實現,例如:http://stackoverflow.com/questions/34434728/how-to-post-json-parameters-using-afnetworking – 2017-02-17 03:43:47

回答

0

您可以使用下面讓輸入什麼你的服務器需要。確保您的服務器將從字典解碼數據

NSMutableDictionary *dicData = [NSMutableDictionary new]; 
    dicData[@"status_timeline"] = @""; 
    dicData[@"with_timeline"] = @""; 
    dicData[@"location_timeline"] = @""; 
    dicData[@"category_timeline"] = @""; 
    dicData[@"privacy_timeline"] = @""; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicData options:NSJSONWritingPrettyPrinted error:nil]; 
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 


    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init]; 
    [dict setValue:@"timeline" forKey:@"do"]; 
    [dict setValue:@"posting" forKey:@"what"]; 
    [dict setValue:@"" forKey:@"session"]; 
    [dict setValue:jsonString forKey:@"data"]; 
    NSLog(@"Your Main Dic: %@", dict); 
+0

謝謝,但字典不發送到服務器 –

+0

你將字典轉換爲JSON對象(字符串),並將json傳遞到服務器。所以只需將服務器檢查並解決您的問題。 – Nirmalsinh

+0

這工作完美。非常感謝! –