2013-07-09 63 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); 
相關問題