将参数字典排序,并转为 GET 参数形式.
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将参数字典排序,并转为 GET 参数形式.相关的知识,希望对你有一定的参考价值。
1.创建 NSString 分类
@interface NSString (WBRequest) - (NSString *)URLEncodedString; - (NSString *)URLEncodedStringWithCFStringEncoding:(CFStringEncoding)encoding; @end @implementation NSString (WBRequest) - (NSString *)URLEncodedString { return [self URLEncodedStringWithCFStringEncoding:kCFStringEncodingUTF8]; } - (NSString *)URLEncodedStringWithCFStringEncoding:(CFStringEncoding)encoding { return (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)[self mutableCopy], NULL, CFSTR("?=,!$&‘()*+;@?\n\"<>#\t :/"), encoding)); } @end
2.将参数字典排序,并转为 GET 参数形式.
+ (NSString *)stringFromDictionaryWithApi:(NSDictionary *)dict { NSArray *sortArray=[dict allKeys]; NSArray *array = [sortArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){ return [obj1 compare:obj2 options:NSNumericSearch]; }]; NSMutableArray *pairs = [NSMutableArray array]; for (int i =0; i<array.count; i++) { NSString * key =[array objectAtIndex:i]; NSString * value =[dict objectForKey:[array objectAtIndex:i]]; if ([[dict objectForKey:[array objectAtIndex:i]] isKindOfClass:[NSString class]]) { value= [value URLEncodedString]; } [pairs addObject:[NSString stringWithFormat:@"%@=%@",key,value]]; } return [pairs componentsJoinedByString:@"&"]; }
以上是关于将参数字典排序,并转为 GET 参数形式.的主要内容,如果未能解决你的问题,请参考以下文章
Pandas使用to_dict函数将dataframe转化为字典(dict)格式数据并指定orientation参数生成不同形式的字典
javajava反射机制,动态获取对象的属性和对应的参数值,并属性按照字典序排序,Field.setAccessible()方法的说明可用于微信支付 签名生成