使用AFHTTPRequestOperation来构造URL(包括大括号)



我需要使用AFNetworking来构造这样的URL,问题是{}如何通过parameter

/api/sth.json?filter[condition]={"53891":[123],"53892":[123,124]}

所以我的代码看起来是这样的(我简化了它):

[self GET:myUrl parameters:@{
                             @"filter" : @{
                                     @"condition" : @{
                                             @"53891" : @[@(123)],
                                             @"53892" : @[@(123),@(124)]}
                                     },
                             } success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                 success(operation,responseObject);
                             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                 failure(operation,error);
                             }];

但它的产量不是预期的:

/api/sth.json?filter[condition][53891][]=123&filter[condition][53892][]=123&filter[condition][53892][]=124

AFHTTPRequestOperation中的parameters中有一种方法可以做到这一点,或者我必须手动将其放入字符串中?

编辑:

我目前的解决方案是这样的:

+(NSString*)convertFromDictionary:(NSDictionary*)dic {
    NSMutableString *outputStr = [NSMutableString new];
    [outputStr appendString:@"{"];
    NSArray *allKeys = [[dic allKeys] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:nil ascending:NO]]];
    for(NSString *key in allKeys) {
        NSArray *objects = dic[key];
        [outputStr appendString:[NSString stringWithFormat:@""%@":[",key]];
        for(NSNumber *nb in objects) {
            [outputStr appendString:[NSString stringWithFormat:@"%li",[nb longValue]]];
            if(![nb isEqual:[objects lastObject]]) {
                [outputStr appendString:@","];
            } else {
                [outputStr appendString:@"]"];
            }
        }
        if(![key isEqual:[allKeys lastObject]]) {
            [outputStr appendString:@","];
        }
    }
    [outputStr appendString:@"}"];
    return outputStr;
}

输入字典所在位置:@{@"53892" : @[@(123),@(124)]}

但这不过是字符串比较。没有比直接使用AFNetworking更聪明的方法了,因为它是相当标准的URL参数?

您想要两种不同的方式来解析字典,但这不能自动完成。正如Mehul之前所说,在创建"参数"字典之前,请尝试序列化参数"条件"(将其值转换为字符串):

NSError *error;
NSDictionary *dict = @{@"53891" : @[@(123)], @"53892" : @[@(123),@(124)]};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSString *params = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[self GET:myUrl parameters:@{
                                 @"filter" : @{
                                 @"condition" : params
                                 },
                         } success:^(AFHTTPRequestOperation *operation, id responseObject) {
                             success(operation,responseObject);
                         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             failure(operation,error);
                         }];

您需要这样做:

  1. 创建词典
  2. 从dictionary创建json字符串(首先使用NSJsonSerialization将dictionary转换为NSData,然后将NSData对象转换为NSString)
  3. 现在,将此字符串以要附加的格式附加到URL中
  4. 从新字符串创建url,并将其传递到get/post方法中,parameters字典为nil

用括号括住数字适用于expressions:

@(6 + x * 2012)

阅读优秀的解释帖子https://stackoverflow.com/a/9349981/3096087

也许括号中的包装在某种程度上混淆了AFHTTPRequestOperation。

NSNumber通过以下方式直接声明:

NSNumber *number = @123;

因此,尝试以下方式使用输入字典:

@{@"53892" : @[@123,@124]}

最新更新