OStack程序员社区-中国程序员成长平台

标题: ios - AFNetworking 2.5.3 数组序列化以显式发送索引 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:04
标题: ios - AFNetworking 2.5.3 数组序列化以显式发送索引

我用

AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer]; 

当数组的格式为:

payments[]
payments[][email]=0&
payments[][category]=&

payments[][email]=1&
payments[][category]=&

我需要类似服务器的索引。

payments[]
payments[0][email]=0&
payments[0][category]=&

payments[1][email]=1&
payments[1][category]=&

我怎样才能做到这一点?

谢谢。



Best Answer-推荐答案


AFHTTPRequestSerializer 有一个方法 setQueryStringSerializationWithBlock它允许您提供自己的 block 来序列化参数。

很遗憾,the internal AF* functions for serialization是私有(private)的,但您可以复制它们并在 these lines 中进行小修改添加索引。

设置你自己的序列化 block :

[serializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError *__autoreleasing *error) {
    return ZAFQueryStringFromParametersWithEncoding(parameters, serializer.stringEncoding);
}];

包括代码,其中 AFNetworking 函数已被复制并以 Z 为前缀到 ZAF:

static NSString * const kAFCharactersToBeEscapedInQueryString = @":/?&=;+!@#$()',*";

static NSString * AFPercentEscapedQueryStringKeyFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
    static NSString * const kAFCharactersToLeaveUnescapedInQueryStringPairKey = @"[].";

    return (__bridge_transfer  NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, (__bridge CFStringRef)kAFCharactersToLeaveUnescapedInQueryStringPairKey, (__bridge CFStringRef)kAFCharactersToBeEscapedInQueryString, CFStringConvertNSStringEncodingToEncoding(encoding));
}

static NSString * AFPercentEscapedQueryStringValueFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
    return (__bridge_transfer  NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, (__bridge CFStringRef)kAFCharactersToBeEscapedInQueryString, CFStringConvertNSStringEncodingToEncoding(encoding));
}

#pragma mark -

@interface ZAFQueryStringPair : NSObject
@property (readwrite, nonatomic, strong) id field;
@property (readwrite, nonatomic, strong) id value;

- (id)initWithFieldid)field valueid)value;

- (NSString *)URLEncodedStringValueWithEncodingNSStringEncoding)stringEncoding;
@end

@implementation ZAFQueryStringPair

- (id)initWithFieldid)field valueid)value {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.field = field;
    self.value = value;

    return self;
}

- (NSString *)URLEncodedStringValueWithEncodingNSStringEncoding)stringEncoding {
    if (!self.value || [self.value isEqual:[NSNull null]]) {
        return AFPercentEscapedQueryStringKeyFromStringWithEncoding([self.field description], stringEncoding);
    } else {
        return [NSString stringWithFormat"%@=%@", AFPercentEscapedQueryStringKeyFromStringWithEncoding([self.field description], stringEncoding), AFPercentEscapedQueryStringValueFromStringWithEncoding([self.value description], stringEncoding)];
    }
}

@end

#pragma mark -

static NSArray * ZAFQueryStringPairsFromDictionary(NSDictionary *dictionary);
static NSArray * ZAFQueryStringPairsFromKeyAndValue(NSString *key, id value);

static NSString * ZAFQueryStringFromParametersWithEncoding(NSDictionary *parameters, NSStringEncoding stringEncoding) {
    NSMutableArray *mutablePairs = [NSMutableArray array];
    for (ZAFQueryStringPair *pair in ZAFQueryStringPairsFromDictionary(parameters)) {
        [mutablePairs addObject:[pair URLEncodedStringValueWithEncoding:stringEncoding]];
    }

    return [mutablePairs componentsJoinedByString"&"];
}

NSArray * ZAFQueryStringPairsFromDictionary(NSDictionary *dictionary) {
    return ZAFQueryStringPairsFromKeyAndValue(nil, dictionary);
}

NSArray * ZAFQueryStringPairsFromKeyAndValue(NSString *key, id value) {
    NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey"description" ascending:YES selectorselector(compare];

    if ([value isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dictionary = value;
        // Sort dictionary keys to ensure consistent ordering in query string, which is important when deserializing potentially ambiguous sequences, such as an array of dictionaries
        for (id nestedKey in [dictionary.allKeys sortedArrayUsingDescriptors[ sortDescriptor ]]) {
            id nestedValue = [dictionary objectForKey:nestedKey];
            if (nestedValue) {
                [mutableQueryStringComponents addObjectsFromArray:ZAFQueryStringPairsFromKeyAndValue((key ? [NSString stringWithFormat"%@[%@]", key, nestedKey] : nestedKey), nestedValue)];
            }
        }
    } else if ([value isKindOfClass:[NSArray class]]) {
        NSArray *array = value;
        NSInteger idx = 0;
        for (id nestedValue in array) {
            [mutableQueryStringComponents addObjectsFromArray:ZAFQueryStringPairsFromKeyAndValue([NSString stringWithFormat"%@[%ld]", key, idx++], nestedValue)];
        }
    } else if ([value isKindOfClass:[NSSet class]]) {
        NSSet *set = value;
        for (id obj in [set sortedArrayUsingDescriptors[ sortDescriptor ]]) {
            [mutableQueryStringComponents addObjectsFromArray:ZAFQueryStringPairsFromKeyAndValue(key, obj)];
        }
    } else {
        [mutableQueryStringComponents addObject:[[ZAFQueryStringPair alloc] initWithField:key value:value]];
    }

    return mutableQueryStringComponents;
}

关于ios - AFNetworking 2.5.3 数组序列化以显式发送索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118388/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4