tech_human tech_human - 1 month ago 5
Objective-C Question

Parsing JSON in Objective-C using NSPredicates on Array

I have the following JSON within an array(array name - banksList) from which I want to get the branch names of "ORIENTAL BANK OF COMMERCE":

<__NSArrayM 0x600000a4e820>(
{
ADDRESS = "PLOT NO.C/3/10-B,GIDCOPP. ASHOPALAV GUEST HOUSE,";
BANK = "BANK OF INDIA";
BRANCH = "ANKLESHWAR INDUSTRIAL ESTATE";
CITY = ANKALESHWAR;
CONTACT = 2646226779;
DISTRICT = ANKALESHWAR;
IFSC = BKID0003003;
"MICR CODE" = 393013051;
STATE = GUJARAT;
"_id" = 57cad024eae65540465771e5;
},
{
ADDRESS = "C-5/1 GIDC RAVI KIRAN COMPX,VARIA CHATR RASTE,RAVI KIRAN COMPX, NH-8,ANKALESHWAR";
BANK = "ORIENTAL BANK OF COMMERCE";
BRANCH = "ANKLESHWAR-GUJRAT";
CITY = ANKALESHWAR;
CONTACT = 222123;
DISTRICT = ANKALESHWAR;
IFSC = ORBC0100736;
"MICR CODE" = 393022051;
STATE = GUJARAT;
"_id" = 57cad02aeae655404658565c;
},
{
ADDRESS = "KENDRIYA VIDYALAYA,13,,GANDHAR TOWN,ONGC TOWN,ANKALESHWAR";
BANK = "ORIENTAL BANK OF COMMERCE";
BRANCH = "ANKLESHWAR-EC KENDRIYA VIDYALAY";
CITY = ANKALESHWAR;
CONTACT = 232399;
DISTRICT = ANKALESHWAR;
IFSC = ORBC0105134;
"MICR CODE" = 393022052;
STATE = GUJARAT;
"_id" = 57cad02aeae6554046585c43;
}
)


I tried doing:

NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF.bank contains %@", @"ORIENTAL BANK OF COMMERCE"];
banksList = [banksList filteredArrayUsingPredicate:bPredicate];
branchNamesArray = [banksList valueForKey:@"BRANCH"];


My predicate doesn't seem to be working. Whenever I do filteredArrayUsingPredicate I get empty array back. What am I doing wrong?

Answer

Keys are case-sensitive. Your predicate needs to be:

@"self.BANK contains %@"
Comments