iOS 无法将字典值获取到数组

iOS Can't get Dictionary values to an array

我有字典:

{
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);

File.txt : { "field": [ { "id": 6, "name": "Andrea" }, { "id": 67, "name": "Francesco" }, { "id": 8, "name": "Maria" }, { "id": 6, "name": "Paolo" }, { "id": 67, "name": "Sara" } ] }

最可能的原因是您的 NSDictionary 使用整数而不是 NSString 作为键。在这种情况下,这应该有效:

NSArray *arrayNew= [result objectForKey:@67]; // No quotes
NSMutableArray *arrayNew = [[NSMutableArray alloc] init];
NSArray *commentArray = [dictionary valueForKey:@"67"];
for (NSDictionary *commentDic in commentArray) {

                Model *modelObj = [[Model alloc] init];   
                modelObj = [Model modelFromDictionary:commentDic];
                [arrayNew addObject:modelObj]
        }

此外,您还必须创建模型 .h 和 .m class 才能进行映射。 modelFromDictionary 方法将进行映射。

.m

@implementation Version

- (NSDictionary *)jsonMapping {

return [[NSDictionary alloc] initWithObjectsAndKeys:
        @"id",@"id",
        @"name",@"name",
        nil];
}

+ (Model *)modelFromDictionary:(NSDictionary *)dictionary {

Model *model = [[Model alloc] init];

NSDictionary *mapping = [model jsonMapping];

for(NSString *attribute in [mapping allKeys]) {

    NSString *classProperty = [mapping objectForKey:attribute];
    NSString *attributeValue = [dictionary objectForKey:attribute];

    if(attributeValue != nil && ![attributeValue isKindOfClass:[NSNull class]]) {

        [model setValue:attributeValue
                 forKey:classProperty];
    }

}
return model;
}

.h

@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *name;



+ (Model *)modelFromDictionary:(NSDictionary *)dictionary;

终于参考了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);

File.txt :

{
"field": [
{
"id": 6,
"name": "Andrea"
},
{
"id": 67,
"name": "Francesco"
},
{
"id": 8,
"name": "Maria"
},
{
"id": 6,
"name": "Paolo"
},
{
"id": 67,
"name": "Sara"
}
]
}

我的最终结果:

ID arr:(
6,
67,
8
)

 Name arr:(
        (
        Andrea,
        Paolo
    ),
        (
        Francesco,
        Sara
    ),
        (
        Maria
    )
)