我有字典:
{
6 = (
{
id = 6;
name = Andrea;
},
{
id = 6;
name = Paolo;
}
);
8 = (
{
id = 8;
name = Maria;
}
);
67 = (
{
id = 67;
name = Francesco;
},
{
id = 67;
name = Sara;
}
);
}
我试图将值放入数组中。
我的代码是:
NSArray *arrayNew= [result valueForKey"67"];
NSLog(@"Hello:%@",arrayNew);
但我总是得到空值。
我的完整代码:
NSMutableArray *idArray=[[NSMutableArray alloc]init];
NSString* path = [[NSBundle mainBundle] pathForResource"File"
ofType"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSMutableData *results11 = [content JSONValue];
NSLog(@"Check:%@",results11);
NSArray *array= [results11 valueForKeyPath"field"];
NSMutableDictionary* result = [NSMutableDictionary dictionary];
for (NSDictionary* dict in array)
{
NSNumber* anID = [dict objectForKey"id"];
if (![idArray containsObject:[dict objectForKey"id"]]) {
// do something
[idArray addObject:[dict objectForKey"id"]];
}
NSMutableArray* resultsForID = [result objectForKey:anID];
if (!resultsForID)
{
resultsForID = [NSMutableArray array];
[result setObject:resultsForID forKey:anID];
}
[resultsForID addObject:dict];
}
NSLog(@"Result:%@",result);
NSLog(@"ID arr:%@",idArray);
// NSArray *temp= [results11 valueForKeyPath"Response.alertHistory"];
NSString *arrayNew= [result valueForKeyPath"67"];
NSLog(@"Hello:%@",arrayNew);
文件.txt : {
“ field ”: [
{
“身份证”:6,
“名称”:“安德烈亚”
},
{
“身份证”:67,
“名称”:“弗朗西斯科”
},
{
“身份证”:8,
“名字”:“玛丽亚”
},
{
“身份证”:6,
“名称”:“保罗”
},
{
“身份证”:67,
“名称”:“萨拉”
}
]
}
最后引用 Tommy 的解决方案,我解决了我的问题。
NSArray *commentArray = result[@67];
还有我的最终代码:
NSMutableArray *idArray=[[NSMutableArray alloc]init];
NSString* path = [[NSBundle mainBundle] pathForResource"File"
ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSMutableData *results11 = [content JSONValue];
NSLog(@"Check:%@",results11);
NSArray *array= [results11 valueForKeyPath:@"field"];
NSMutableDictionary* result = [NSMutableDictionary dictionary];
for (NSDictionary* dict in array)
{
NSNumber* anID = [dict objectForKey:@"id"];
if (![idArray containsObject:[dict objectForKey:@"id"]]) {
// do something
[idArray addObject:[dict objectForKey:@"id"]];
}
NSMutableArray* resultsForID = [result objectForKey:anID];
if (!resultsForID)
{
resultsForID = [NSMutableArray array];
[result setObject:resultsForID forKey:anID];
}
[resultsForID addObject:dict];
}
NSLog(@"Result:%@",result);
NSMutableArray *arrayNew = [[NSMutableArray alloc] init];
for (int i=0; i<[idArray count]; i++) {
NSArray *commentArray = result[idArray[i]];
NSLog(@"COMM:%@",commentArray);
NSMutableArray *arr=[[NSMutableArray alloc]init];
for (NSDictionary* dict in commentArray)
{
[arr addObject:[dict objectForKey:@"name"]];
}
[arrayNew addObject:arr];
}
NSLog(@"ID arr:%@",idArray);
NSLog(@"Name arr:%@",arrayNew);
文件.txt:
{
"field": [
{
"id": 6,
"name": "Andrea"
},
{
"id": 67,
"name": "Francesco"
},
{
"id": 8,
"name": "Maria"
},
{
"id": 6,
"name": "aolo"
},
{
"id": 67,
"name": "Sara"
}
]
}
我的最终结果:
ID arr
6,
67,
8
)
Name arr
(
Andrea,
Paolo
),
(
Francesco,
Sara
),
(
Maria
)
)
关于iOS无法将字典值获取到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28358787/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |