面对一些挑战,有没有专家可以帮助我。我面临的问题基本上是把数组字典转换成一个不同对象的字典,这听起来让我很困惑,所以请任何逻辑大师帮助我。我有一组这种格式的记录,
originalArray = [{
"valid": "Y",
"mof": "ON",
"dof": "17-05-2019",
"rtntype": "CODE1",
"ret_prd": "042019",
},
{
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE1",
"ret_prd": "032019",
},
{
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE2",
"ret_prd": "032019",
}
]
我想创建以下格式的字典,请帮助我。
{
"032019" = {
"CODE1" = {
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE1",
"ret_prd": "032019",
},
"CODE2" = {
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE2",
"ret_prd": "032019",
"status": "Filed"
}
},
"042019" = {
"CODE1" = {
"valid": "Y",
"mof": "ON",
"dof": "17-05-2019",
"rtntype": "CODE1",
"ret_prd": "042019",
}
}
}
这是我的样品,你可以自己检查
NSArray *array = @[
@{@"valid":@"Y",@"mof":@"ON",@"dof":@"17-05-2019",@"rtntype":@"CODE1",@"ret_prd":@"042019"},
@{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE1",@"ret_prd":@"032019"},
@{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE2",@"ret_prd":@"032019"}
];
NSMutableDictionary *result = [NSMutableDictionary new];
for (int i = 0; i < array.count; i++) {
NSDictionary *dict = array[i];
NSString *value = dict[@"ret_prd"];
if (result[value] == nil) {
NSArray *filterArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(ret_prd contains[c] %@)", value]];
NSMutableDictionary *filterDict = [NSMutableDictionary new];
for (int i = 0; i < filterArray.count; i++) {
filterDict[filterArray[i][@"rtntype"]] = filterArray[i];
}
result[value] = filterDict;
}
}
NSLog(@"%@", result);
控制台日志的结果:
https://i.stack.imgur.com/WPEBH.png
很长时间没有代码目标-C,所以上面的代码可能很奇怪。如果你不知道在哪里,我会给你解释的。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。