菜鸟教程小白 发表于 2022-12-13 07:04:43

ios - AFNetworking 2.5.3 数组序列化以显式发送索引


                                            <p><p>我用</p>

<pre><code>AFHTTPRequestSerializer *serializer = ;
</code></pre>

<p>当数组的格式为:</p>

<pre><code>payments[]
payments[]=0&amp;
payments[]=&amp;

payments[]=1&amp;
payments[]=&amp;
</code></pre>

<p>我需要类似服务器的索引。</p>

<pre><code>payments[]
payments=0&amp;
payments=&amp;

payments=1&amp;
payments=&amp;
</code></pre>

<p>我怎样才能做到这一点?</p>

<p>谢谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>AFHTTPRequestSerializer</code> 有一个方法 <a href="https://github.com/AFNetworking/AFNetworking/blob/2.5.3/AFNetworking/AFURLRequestSerialization.h#L195" rel="noreferrer noopener nofollow"><code>setQueryStringSerializationWithBlock</code></a>它允许您提供自己的 block 来序列化参数。 </p>

<p>很遗憾,<a href="https://github.com/AFNetworking/AFNetworking/blob/2.5.3/AFNetworking/AFURLRequestSerialization.m#L65-L159" rel="noreferrer noopener nofollow">the internal <code>AF*</code> functions for serialization</a>是私有(private)的,但您可以复制它们并在 <a href="https://github.com/AFNetworking/AFNetworking/blob/2.5.3/AFNetworking/AFURLRequestSerialization.m#L144-L148" rel="noreferrer noopener nofollow">these lines</a> 中进行小修改添加索引。 </p>

<p>设置你自己的序列化 block :</p>

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

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

<pre><code>static NSString * const kAFCharactersToBeEscapedInQueryString = @&#34;:/?&amp;=;+!@#$()&#39;,*&#34;;

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

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

static NSString * AFPercentEscapedQueryStringValueFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
    return (__bridge_transferNSString *)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)initWithField:(id)field value:(id)value;

- (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding;
@end

@implementation ZAFQueryStringPair

- (id)initWithField:(id)field value:(id)value {
    self = ;
    if (!self) {
      return nil;
    }

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

    return self;
}

- (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding {
    if (!self.value || ]) {
      return AFPercentEscapedQueryStringKeyFromStringWithEncoding(, stringEncoding);
    } else {
      return , stringEncoding), AFPercentEscapedQueryStringValueFromStringWithEncoding(, 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 = ;
    for (ZAFQueryStringPair *pair in ZAFQueryStringPairsFromDictionary(parameters)) {
      ];
    }

    return ;
}

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

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

    NSSortDescriptor *sortDescriptor = ;

    if (]) {
      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 ]) {
            id nestedValue = ;
            if (nestedValue) {
                &#34;, key, nestedKey] : nestedKey), nestedValue)];
            }
      }
    } else if (]) {
      NSArray *array = value;
      NSInteger idx = 0;
      for (id nestedValue in array) {
            &#34;, key, idx++], nestedValue)];
      }
    } else if (]) {
      NSSet *set = value;
      for (id obj in ]) {
            ;
      }
    } else {
       initWithField:key value:value]];
    }

    return mutableQueryStringComponents;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - AFNetworking 2.5.3 数组序列化以显式发送索引,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/30118388/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/30118388/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - AFNetworking 2.5.3 数组序列化以显式发送索引