2015-02-06 60 views
2

我有词典:的iOS无法获取字典值数组

{ 
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" } ] }

+0

可以u显示一些额外的代码 – 2015-02-06 04:53:47

+0

好 我会加。 – PREMKUMAR 2015-02-06 04:55:52

+0

试试这个:'NSArray * arrayNew =(NSArray *)result [@“67”];' – 2015-02-06 04:56:39

回答

1

我解决我的问题。

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 
    ) 
) 
0

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

NSArray *arrayNew= [result objectForKey:@67]; // No quotes 
+0

它的崩溃。 [__NSCFNumber长度]:无法识别的选择器发送到实例0x7b649470 – PREMKUMAR 2015-02-06 05:01:09

+1

@PREMKUMAR您可以尝试'objectForKey'来代替吗? – dasblinkenlight 2015-02-06 05:02:36

+0

......甚至是结果[@ 67]'。但要采取暗示的观点:如果你不知道两者的区别,那么肯定不需要'valueForKey:'。 – Tommy 2015-02-06 05:14:34

0
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类进行映射。 和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; 
+0

你可以发送model.h和model.m文件吗? – PREMKUMAR 2015-02-06 05:02:16