2017-02-17 83 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

这工作完美。非常感谢! –