i use
afhttprequestserializer *serializer = [afhttprequestserializer serializer];
and sends out call server when having array in format:
payments[] payments[][email]=0& payments[][category]=& payments[][email]=1& payments[][category]=&
i need indexes server like.
payments[] payments[0][email]=0& payments[0][category]=& payments[1][email]=1& payments[1][category]=&
how can achieve that?
thanks.
afhttprequestserializer
has method setquerystringserializationwithblock
allows provide own block serializing parameters.
unfortunately, the internal af*
functions serialization private, can copy them , make small modification in these lines add indexes.
to set own serialization block:
[serializer setquerystringserializationwithblock:^nsstring *(nsurlrequest *request, id parameters, nserror *__autoreleasing *error) { return zafquerystringfromparameterswithencoding(parameters, serializer.stringencoding); }];
including code, afnetworking functions have been copied , prefixed 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)initwithfield:(id)field value:(id)value; - (nsstring *)urlencodedstringvaluewithencoding:(nsstringencoding)stringencoding; @end @implementation zafquerystringpair - (id)initwithfield:(id)field value:(id)value { self = [super init]; if (!self) { return nil; } self.field = field; self.value = value; return self; } - (nsstring *)urlencodedstringvaluewithencoding:(nsstringencoding)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]; (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 selector:@selector(compare:)]; if ([value iskindofclass:[nsdictionary class]]) { nsdictionary *dictionary = value; // sort dictionary keys ensure consistent ordering in query string, important when deserializing potentially ambiguous sequences, such array of dictionaries (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; (id nestedvalue in array) { [mutablequerystringcomponents addobjectsfromarray:zafquerystringpairsfromkeyandvalue([nsstring stringwithformat:@"%@[%ld]", key, idx++], nestedvalue)]; } } else if ([value iskindofclass:[nsset class]]) { nsset *set = value; (id obj in [set sortedarrayusingdescriptors:@[ sortdescriptor ]]) { [mutablequerystringcomponents addobjectsfromarray:zafquerystringpairsfromkeyandvalue(key, obj)]; } } else { [mutablequerystringcomponents addobject:[[zafquerystringpair alloc] initwithfield:key value:value]]; } return mutablequerystringcomponents; }
Comments
Post a Comment