ios 数组中的字典排序_iOS 加签前对嵌套多层字典数组排序组装

+(void)enumerateKeysAndObjectsUsingBlock:(NSDictionary*)dic block:(void (^)(id key, id obj))block{

NSArray *allKey = [[dic allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

return [obj1 compare:obj2 options:NSLiteralSearch];

}];

for (int i=0;i

id obj = dic[allKey[i]];

id key = allKey[i];

block(key,obj);

}

}

+ (NSString*)handleDic:(NSDictionary*)data

{

NSMutableString *resultStr = [[NSMutableString alloc]initWithCapacity:0];

//获取排好序的字典 blcok 返回key和Value

[[self class] enumerateKeysAndObjectsUsingBlock:data block:^(id key, id obj) {

//根据类型判断 针对每种类型处理

if ([obj isKindOfClass:[NSDictionary class]]) { // 是字典对象

obj = [[self class] handleDic:obj];

//组装字典

obj = [NSString stringWithFormat:@"{%@}",[[NSString stringWithFormat:@"%@",obj] substringToIndex:[NSString stringWithFormat:@"%@",obj].length-1]];

}

if ([obj isKindOfClass:[NSArray class]]) {// 是数组对象

NSMutableString *resobj = [NSMutableString new];

for (NSDictionary *dict in obj) {

obj = [[self class] handleDic:dict];

//组装每一个字典

[resobj appendFormat:@"%@,",[NSString stringWithFormat:@"{%@}",obj]];

}

//给数组拼接中括号

obj= [NSString stringWithFormat:@"[%@]",[resobj substringToIndex:resobj.length-1]];

}

if ([obj isKindOfClass:[NSString class]] || [obj isKindOfClass:[NSNumber class]]) {//字符串数字类型

//拼接字符串和数字类型

[resultStr appendFormat:@"%@=%@&",key,obj];

}

}];

return [resultStr substringToIndex:resultStr.length-1];

}

使用

NSDictionary *dic = @{

@"code":@0,

@"list":@[@{

@"userName":@"wjx",

@"sex":@"男"

},

@{

@"userName":@"www",

@"sex":@"男"

},@{

@"userName":@"jjj",

@"sex":@"男"

}],

@"data":@{

@"userinfo":@{

@"roleNo":@"0000000002",

@"lastLoginTime":@"2020-11-24 17:25:04",

@"roleFlag":@"1",

@"userName":@"网点社操作员",

@"level":@{

@"normal":@"1",

@"vip":@{

@"start":@"1",

@"time":@"2020-02-02",

@"nums":@[@{@"b":@"1",@"a":@"3"},@{@"a":@"2"}]

},

@"svip":@"3"

}

},

@"token":@"5d60ab6bf0d64899952f38bdb7042010"

},

@"message":@"操作成功",

@"status":@"success"

};

NSString *sgin = [HTSignatureUtil handleDic:dic];

NSLog(@"%@",sgin);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值