如何通过iOS中的AFNetworking以JSON格式发送NSDate?

时间:2016-08-29 05:50:13

标签: ios json nsdate afnetworking

如何通过iOS中的AFNetworking以JSON格式发送NSDate? Json请求 -

{
    "from_date" : "11/11/2014",
    "to_date" : "11/11/2017"
}

代码 -

// Formats the date chosen with the date picker.
- (NSString *)formatDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"dd'/'MM'/'yyyy"];
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    return formattedDate;
}
    - (void)updateToDateField:(id)sender
    {
        UIDatePicker *picker = (UIDatePicker*)toDateField.inputView;
        toDateField.text = [self formatDate:picker.date];
    }
    // Storing the to date in NSString
        dashboard.toDateString = toDateField.text;

3 个答案:

答案 0 :(得分:0)

NSDate转换为String并发送,就像发送其他参数一样。

答案 1 :(得分:0)

试试这个

 NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];

现在在你的json中使用dateString

答案 2 :(得分:0)

我认为您忘记在使用AFNetworking Manage类调用API时序列化您的请求。

尝试在代码中添加以下行。

    AFHTTPRequestOperationManager *manager;
     manager = [AFHTTPRequestOperationManager manager];
     manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:strRequestUrl parameters:dictParams success:^(AFHTTPRequestOperation *operation, id responseObject)
        {

            NSString *msg;
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject
                                                               options:NSJSONWritingPrettyPrinted
                                                                 error:nil];
            NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


        } failure:^(AFHTTPRequestOperation *operation, NSError *error)
        {

            NSLog(@"request : %@",operation.request);
            NSLog(@"Error is %@",error);

        }];