-2

I have a little problem, I have a json feed and I would like to display in an array that items whose Country = France How to do ? Thank you
(Objective-C)
My JSON :

    { 
  "menu": "Fichier", 
  "commandes": [ 
      {
          "country": "France", 
          "city": "Paris"
      }, 
      {
          "country": "USA", 
          "city": "New York"
      }, 
      {
          "country": "Canada",
          "city": "Quebec"
      }
   ] 
} 
victor bill
  • 209
  • 2
  • 15
  • Please check this link and I hope it helps [Filtering the NSMutableArray](http://stackoverflow.com/questions/110332/filtering-nsarray-into-a-new-nsarray-in-objective-c) – Pradheep Narendran P Jan 21 '16 at 13:39

3 Answers3

1

First parse your JSON data using NSJSONSerialization

// data will be your Json NSData

 NSError* jsonError;
 NSDictionary* jsonDictn = [NSJSONSerialization JSONObjectWithData:data
                             options:kNilOptions
                             error:&error];
 NSMutableArray * aray = [[jsonDictn objectForKey:@"commandes"] mutableCopy];
 for (NSDictionary* tmpdictn in aray) {
     NSString * tmpstr = [tmpdictn objectForKey:@"country"];
     if (![tmpstr isEqualToString:@"France"]) {
         [aray removeObject:tmpdictn];
      }
 }
 NSLog(@"aray: %@",aray);
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
  • 1
    If you have already created Array and just want to add filter that use NSPredicate, this code is to get only France Array from actual Json. – Dilip Manek Jan 21 '16 at 14:06
0

This will help:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"commandes" ascending:TRUE];
[sourceArr sortUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
user3071284
  • 6,955
  • 6
  • 43
  • 57
Maulik shah
  • 1,664
  • 1
  • 19
  • 45
0

you need to use NSPredicate to search data from array

use below code

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"country == %@", @"France"];
NSArray *filteredArray = [[arr objectForKey:@"commandes"] filteredArrayUsingPredicate:predicate];
Crazy Developer
  • 3,464
  • 3
  • 28
  • 62