6

I need to construct URL like this using AFNetworking the problem for me it's { and } how to pass throught parameter

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

So my code looks like this (i made it simpler):

[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);
                             }];

But it's produce not expected output:

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

There is a way to do this in parameters in AFHTTPRequestOperation or manually i have to put it into string?

EDIT:

My current solution is like that:

+(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;
}

Where input dictionary is: @{@"53892" : @[@(123),@(124)]}

But it's nothing more than string compare. There is no more clever way to achieve it with AFNetworking directly since it's fairly standard URLs parameters?

Jakub
  • 13,712
  • 17
  • 82
  • 139
  • 1
    Have you set `AFJSONParameterEncoding` to your client with `setParameterEncoding` method ? – Niko Jan 07 '16 at 14:22

3 Answers3

1

You need to do this:

  1. create dictionary
  2. create json string from dictionary(first convert dictionary to NSData using NSJsonSerialization and then convert thata NSData object to NSString)
  3. Now append this string in your URL in format which you want to attach to
  4. create url from new string and pass it in get/post method with paramters dictionary as nil
Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
1

You want 2 different ways of parsing a dictionary, and this cannot be done automatically. As Mehul said before, try serializing your parameter "condition" (converting its value to string) before creating the "parameters" dictionary:

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);
                         }];
nigelman
  • 153
  • 8
0

Wrapping a numbers with parenthesis is for expressions :

@(6 + x * 2012)

Read excellent explanation post https://stackoverflow.com/a/9349981/3096087

Maybe wrapping in parenthesis somehow messes up with AFHTTPRequestOperation.

NSNumber are directly declared the following way :

NSNumber *number = @123;

So try with an input dictionary the following way :

@{@"53892" : @[@123,@124]}
Community
  • 1
  • 1
Niko
  • 3,412
  • 26
  • 35