1

我有NSMutablearray这样的(这是一个JSON)

{
   "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并获取索引。

谁能帮我。

谢谢

4

4 回答 4

7

尝试

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);
于 2013-07-09T08:49:00.890 回答
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 的字典索引。

希望这会帮助你。

于 2013-07-09T08:46:41.717 回答
0

在您的情况下,您的 consol 描述为 Dictionary 并且您可以通过indexOfObjectof获取 Object 的特定索引NSMutableArray

int index = [[myDic objectForKey:@"notification_list"] indexOfObject:yourSearchString];
NSLog(@"index From Array - %d", index);
于 2013-07-09T08:42:47.163 回答
0

首先你必须解析 JSON 并保存到 NSMutableArray。 Parse JSOn 教程: 最后,你在 NSMutableArray 中搜索你想要的类型。

于 2013-07-09T08:45:56.270 回答