2013-07-09 128 views
0

NSMutablearray这样的(这是一个JSON一个)如何获取NSMutablearray中特定对象的索引?

{ 
    "notification_list":[ 
     { 
     "TYPE":"ARTIST", 
     "ID":"07", 
     "DTEXT":"Now you can listen to the songs of ARTIST1", 
     "ADDEDDATE":"27th June, 2013 4:44 pm." 
     }, 
     { 
     "TYPE":"BRAND", 
     "ID":"http:\/\/www.me.lk\/", 
     "DTEXT":"M Entertainment have been joined with Mobile Music", 
     "ADDEDDATE":"27th June, 2013 3:37 pm." 
     }, 
     { 
     "TYPE":"ARTIST", 
     "ID":"03", 
     "DTEXT":"Now you can listen to the songs of ARTIST2", 
     "ADDEDDATE":"27th June, 2013 3:37 pm." 
     }, 
     { 
     "TYPE":"ALBUM", 
     "ID":"Nava Ridgma", 
     "DTEXT":"Get new album of ARTIST3, Nava ridma", 
     "ADDEDDATE":"27th June, 2013 3:37 pm." 
     }, 
     { 
     "TYPE":"SONG", 
     "ID":"05", 
     "DTEXT":"New Song added to the Mobile Music - Hanthane Kandumuduna by ARTIST5, Album - Aradhana", 
     "ADDEDDATE":"27th June, 2013 3:37 pm." 
     } 
    ] 
} 

现在我想要得到这个特定对象的指数在此NSMutablearray

{ 
    "TYPE":"ALBUM", 
    "ID":"Nava Ridgma", 
    "DTEXT":"Get new album of ARTIST3, Nava ridma", 
    "ADDEDDATE":"27th June, 2013 3:37 pm." 
} 

如何我可以从这个搜索这个对象NSMutablearray并获得索引。

任何人都可以帮助我。

感谢

+0

你为什么不采取一些痛苦和传入的JSON转换为对象。一旦你完成了。相信我,生活会容易得多。 – croyneaus4u

回答

2
NSMutableArray *myArray 

考虑到myArray包含您给出的数据。

NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"%K LIKE %@", @"TYPE", @"ALBUM"]; 
NSArray *filteredList = [myArray filteredArrayUsingPredicate:thePredicate]; 

NSDictionary *selectedDict = [[NSDictionary alloc]init]; 
selectedDict = [filteredList lastObject]; 

int index; 
index = [arr indexOfObject:selectedDict]; 

这会给你带有type = album的字典索引。

希望这会帮助你。

0

在你的情况,你的CONSOL描述为一个字典,你可以通过indexOfObject得到NSMutableArray

int index = [[myDic objectForKey:@"notification_list"] indexOfObject:yourSearchString]; 
NSLog(@"index From Array - %d", index); 
0

对象的特定指数首先必须解析JSON并保存到NSMutableArray里。 Parse JSOn tutorial: 最后,你在NSMutableArray中搜索你想要的类型。

6

尝试

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TYPE == [cd] %@",@"ALBUM"]; 

NSInteger index = [array indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { 
    return [predicate evaluateWithObject:obj]; 
}]; 

NSLog(@"Index of object %d",index); 
相关问题