How to get data into NSArray from NSDictionary - iphone
i am getting following error while getting data from dictionary
"-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x4fbb80"
here is my code
NSDictionary *dic = [XMLReader dictionaryForXMLString:class.returnData error:nil];
MyGiftsarray = [[dic objectForKey:#"response"] objectForKey:#"gifts"];
for (NSDictionary *element in MyGiftsarray) {
[MyGiftsnamearray addObject:[element objectForKey:#"name"]];
[MyGiftsChkarray addObject:[element objectForKey:#"chk"]];
[MyGiftsIdarray addObject:[element objectForKey:#"id"]];
[MyGiftsApprovedarray addObject:[element objectForKey:#"approved"]];
[MyGiftsisCompletedarray addObject:[element objectForKey:#"completed"]];
}
this is NSDictionary from which i try to get data.
MyGiftsDict->(
{
Appliedpoint = 500;
Requiredpoint = 5000;
approved = N;
chk = 50;
completed = 0;
id = 66;
name = "1-800Flowers.com";
},
{
Appliedpoint = 85;
Requiredpoint = 2500;
approved = N;
chk = 25;
completed = 0;
id = 71;
name = "Bath Body Works";
},
{
Appliedpoint = 5;
Requiredpoint = 2500;
approved = N;
chk = 25;
completed = 0;
id = 75;
name = "Buca di Beppo";
},
{
Appliedpoint = 36;
Requiredpoint = 2500;
approved = N;
chk = 25;
completed = 0;
id = 66;
name = "1-800Flowers.com";
},
{
Appliedpoint = 25;
Requiredpoint = 5000;
approved = N;
chk = 50;
completed = 0;
id = 90;
name = "Jelly Belly";
},
{
Appliedpoint = 120;
Requiredpoint = 500;
approved = N;
chk = 5;
completed = 0;
id = 129;
name = "Amazon.com";
}
)
You are iterating in wrong manner-
Use -
MyGiftsDict = [[dic objectForKey:#"response"] objectForKey:#"gifts"];
And then iterate over dictionary step by step.
You need to use something like -
for(NSString *key in [MyGiftsDict allKeys])
{
[MyGiftsnamearray addObject:[MyGiftsDict objectForKey:key]];
}
Gifts is an NSDictionary in itself. It's not an NSArray! You don't need that for loop. Use simply
[MyGiftsNameArray addObject:[MyGiftsArray objectForKey:#"name"];
et cetera.
I don't think you will need the forcycle.
Related
How to filter array of NSDictionary for key separated by comma
I have an array of NSDictionary.NSDictionary has a key named as multiple_image key that contain string separated by ,. I want set of array that contain 123.png for multiple_images key. Can some one show me how to do this using NSPredicate or without predicate. //Array { Image = "<UIImage: 0xf72df30>"; active = yes; "admin_id" = 169; "category_id" = 32; "chef_id" = 175; descr = "Cool tea to cool down the mind."; id = 110; "multiple_images" = "Jellyfish.jpg,345.png"; name = "Southern Sweet Ice Tea"; price = 160; rating = 3; selected = 0; "subcat_id" = 23; "tag_id" = 45; "tax_id" = 10; "tax_value" = "12.00"; }, { Image = "<UIImage: 0xf72ebd0>"; active = yes; "admin_id" = 169; "category_id" = 31; "chef_id" = 175; descr = "Ingredients are almonds or cashews. No hydrogenated stuff, no extra weirdo ingredients"; id = 107; "multiple_images" = "Jellyfish.jpg,123.png"; name = "Butter Chicken"; price = 300; rating = 3; selected = 0; "subcat_id" = 24; "tag_id" = 43; "tax_id" = 9; "tax_value" = "0.00"; }, { Image = "<UIImage: 0xf72f870>"; active = yes; "admin_id" = 169; "category_id" = 31; "chef_id" = 173; descr = "Raw vegetables including carrots, cucumbers."; id = 100; "multiple_images" = "Jellyfish.jpg,shake.png,"; name = Salads; price = 50; rating = 3; selected = 0; "subcat_id" = 22; "tag_id" = 44; "tax_id" = 9; "tax_value" = "0.00"; }
Using predicates, [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"multiple_images CONTAINS '123'"]]; Using predicates, but with blocks NSArray *filtered = [test filteredArrayUsingPredicate:[NSPredicate predicateWithBlock: ^BOOL(NSDictionary *evaluatedObject, NSDictionary * bindings) { NSString *key = #"123"; return ([evaluatedObject[#"multiple_images"] rangeOfString:key].location != NSNotFound); }]];
Try NSDictionary *item = mainArray[0]; NSString *imagesString = item[#"multiple_images"]; NSArray *images = [imagesString componentsSeparatedByString:#","]; Now you can use the images
Try this code for (int i=0; i<[mainArray count]; i++) { NSDictionary *item = [mainArray objectAtIndex:i]; NSString *imagesString = item[#"multiple_images"]; NSArray *images = [imagesString componentsSeparatedByString:#","]; for (int j=0;j<[images count]; j++) { if ([[images objectAtIndex:j] isEqualToString:#"123.png"]) { ///Your Required code; } } }
So you want to save the dictionary that has 123.png. maybe you can try something like this: NSMutableArray *arr = [NSMutableArray new];// your array of objects(dictionaries) __block NSMutableArray *images123 = [NSMutableArray new]; [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSMutableDictionary *dictionary = (NSMutableDictionary *)obj; if([[dictionary allKeys]containsObject:#"multiple_images"]){ NSString *imageList = (NSString *)[dictionary objectForKey:#"multiple_images"]; NSArray *arrayImages = [imageList componentsSeparatedByString:#","]; if([arrayImages containsObject:#"123.png"]){ [images123 addObject:dictionary]; } } }];
how to select image from JSON parsed in Array
I am using JSON parsing, I put data in array named JSON Array. In console it looks like JSON Array=( { "created_by" = 42; "created_on" = "2012-02-29 11:23:37"; "file_description" = ""; "file_is_downloadable" = 0; "file_is_forSale" = 0; "file_is_product_image" = 1; "file_meta" = ""; "file_mimetype" = "image/jpeg"; "file_params" = ""; "file_title" = "hand shovel"; "file_type" = product; "file_url" = "images/stories/virtuemart/product/cca3cd5db813ee6badf6a3598832f2fc.jpg"; "file_url_thumb" = "images/stories/virtuemart/product/resized/cca3cd5db813ee6badf6a3598832f2fc_90x90.jpg"; "locked_by" = 0; "locked_on" = "0000-00-00 00:00:00"; "modified_by" = 42; "modified_on" = "2012-02-29 11:23:37"; ordering = 0; "product_name" = "Hand Shovel"; published = 0; shared = 0; "virtuemart_category_id" = 1; "virtuemart_media_id" = 13; "virtuemart_product_id" = 1; "virtuemart_vendor_id" = 1; }, { "created_by" = 42; "created_on" = "2012-02-29 11:35:09"; "file_description" = ""; "file_is_downloadable" = 0; "file_is_forSale" = 0; "file_is_product_image" = 1; "file_meta" = ""; "file_mimetype" = "image/jpeg"; "file_params" = ""; "file_title" = "our ladder"; "file_type" = product; "file_url" = "images/stories/virtuemart/product/8cb8d644ef299639b7eab25829d13dbc.jpg"; "file_url_thumb" = "images/stories/virtuemart/product/resized/8cb8d644ef299639b7eab25829d13dbc_90x90.jpg"; "locked_by" = 0; "locked_on" = "0000-00-00 00:00:00"; "modified_by" = 42; "modified_on" = "2012-02-29 11:35:09"; ordering = 0; "product_name" = Ladder; published = 0; shared = 0; "virtuemart_category_id" = 3; "virtuemart_media_id" = 8; "virtuemart_product_id" = 2; "virtuemart_vendor_id" = 1; }, Now from this i need to select "file_url_thumb" which is actually image in seperate array. please let me know how can i do this?
//Your array has two dictionary .. for(NSMutableDictionary *dict in JSON_Array){ NSString *urlString = [dict objectForKey:#"file_url_thumb"]; if(urlString){ NSURL *url = [NSURL urlWithString:urlString]; NSData* imageData = [[NSData alloc] initWithContentsOfURL:url]]; UIImage* image = [[UIImage alloc] initWithData:imageData]; //show your image on imageview } } Hope this will help you
NSString *str_URL =[ [json objectAtIndex:anIndex] valueForKey:#"file_url_thumb"];
NSString *urlString = [[jsonArray objectAtIndex:0] objectForKey:#"file_url_thumb"]; This will give you value for "file_url_thumb" as a string.
how to access all the array of dictionary contents
I have implemented the array of dictionary contents accessing methods but it is access only one content, please help on this Here is my data file <__NSArrayM 0x8b7a350>( { clkDate = "Wednesday, April 11, 2012"; resData = ( { data = 10; independentItem = 0; module = 6; newDate = "11-4-2012"; newTime = "13:31"; seqCounter = 101; sequence = 10007; session = 101; timeframe = Breakfast; timestamp = "2012-04-11 08:01:27 +0000"; title = "Glucose reading : Low"; type = L; }, { data = { hours = 0; minutes = 0; which = "Walking:(null)"; }; independentItem = 1; module = 13; seqCounter = 101; sequence = 10009; session = 101; timestamp = "2012-04-12 08:01:40 +0000"; title = "Low Cause: Increased Exercise"; } ); seqCounter = 101; }, { clkDate = "Thursday, April 12, 2012"; resData = ( { data = 200; independentItem = 0; module = 6; newDate = "12-4-2012"; newTime = "13:31"; seqCounter = 102; sequence = 10017; session = 101; timeframe = Breakfast; timestamp = "2012-04-12 08:01:46 +0000"; title = "Glucose reading : High"; type = H; }, { data = { hours = 0; minutes = 0; which = "Other Light:Kkkkk"; }; independentItem = 1; module = 26; seqCounter = 102; sequence = 10022; session = 101; timestamp = "2012-04-11 18:30:00 +0000"; title = "High Cause: Decreased Exercise"; } ); seqCounter = 102; }, { clkDate = "Thursday, April 12, 2012"; clkIndependentItem = 1; resData = ( { data = { bathing = 1; driving = 1; grocery = 1; meal = 1; }; independentItem = 1; module = 31; seqCounter = 103; sequence = 10035; session = 101; timestamp = "2012-04-12 08:02:32 +0000"; title = "High Cause: Decreased activity"; } ); seqCounter = 103; } ) In above data file i need to access "independentItem = 1" related items i tried source code is for (int i = 0; i < [self.gluClkDetailArray count]; i++) { NSMutableDictionary *mDict = [self.gluClkDetailArray objectAtIndex:i]; { NSMutableArray *mResData = [mDict objectForKey:#"resData"]; NSDate *mTimestamp = [[mResData objectAtIndex:0] objectForKey:#"timestamp"]; NSDateFormatter *mFormatter = [[NSDateFormatter alloc] init]; [mFormatter setDateStyle:NSDateFormatterFullStyle]; NSString *mClkDate = [mFormatter stringFromDate:mTimestamp]; [mDict setValue:mClkDate forKey:#"clkDate"]; NSString *mIndItem = [[mResData objectAtIndex:0] objectForKey:#"independentItem"]; if (mIndItem.intValue == 1) { [mDict setValue:mIndItem forKey:#"clkIndependentItem"]; [mIndpendentArray addObject:mDict]; } [gluClkResultArray addObject:mDict]; } when ever i put the mResData objectAtIndex:0 instead of i, app got crashed. i have saved array of dictionary values based on the sequence counter Thanks in advance
Change NSString *mIndItem = [[mResData objectAtIndex:0] objectForKey:#"independentItem"]; to that: NSString *mIndItem = [[mResData objectAtIndex:1] objectForKey:#"independentItem"]; The independentItem is in the dictionary that in at index 1, not 0. Hope it helps
convert dictionary to array after removing key values
i need to remove first three keys values from each index of array. What i did is for (int i=0; i<[normalscoringarray count]; i++) { NSDictionary *dictionary = [normalscoringarray objectAtIndex:i]; NSMutableDictionary *mutableDictionary = [dictionary mutableCopy]; [mutableDictionary removeObjectForKey:#"cn"]; [mutableDictionary removeObjectForKey:#"gdate"]; [mutableDictionary removeObjectForKey:#"gid"]; NSLog(#"mutableDictionary.....%#",mutableDictionary); NSMutableArray *remainingArray = [[NSMutableArray alloc]init]; for (id item in mutableDictionary) { [remainingArray setArray:[mutableDictionary objectForKey:item]]; } } and it does not give remainingArray in that order i want, more over it removes all keys. normalscoringarray....( { cn = "Ranjit Garh"; gdate = "2012-03-25"; gid = 1; id = 1; p1nets = "3,3,4,4,2,2,2,4,3,3,4,2,5,3,5,4,3,4||27||33||60"; p1nh = "BP||14||1"; p1score = "4,4,5,5,3,3,3,5,4,4,5,3,5,3,6,4,4,5||36||39||75||61"; p1spoints = "3,3,3,2,3,4,3,3,3,3,2,4,1,2,2,1,3,3||27||21||48"; p2nets = "4,3,4,4,3,4,2,5,4,4,3,3,4,3,6,3,4,4||33||34||67"; p2nh = "DG||18||2"; p2score = "5,4,5,5,4,5,3,6,5,5,4,4,5,4,7,4,5,5||42||43||85||67"; p2spoints = "2,3,3,2,2,2,3,2,2,2,3,3,2,2,1,2,2,3||21||20||41"; p3nets = "4,3,6,4,2,3,2,4,5,4,3,5,6,3,4,3,5,6||33||39||72"; p3nh = "NM||24||3"; p3score = "6,5,7,6,4,5,3,6,6,5,5,6,7,4,5,4,6,7||48||49||97||73"; p3spoints = "2,3,1,2,3,3,3,3,1,2,3,1,0,2,3,2,1,1||21||15||36"; p4nets = "3,4,5,4,2,3,3,5,3,4,3,5,4,3,5,3,4,6||32||37||69"; p4nh = "KS||20||4"; p4score = "5,5,6,5,3,5,4,6,4,5,5,6,5,4,6,4,5,7||43||47||90||70"; p4spoints = "3,2,2,2,3,3,2,2,3,2,3,1,2,2,2,2,2,1||22||17||39"; }, { cn = "Eden Garden"; gdate = "2012-03-26"; gid = 2; id = 2; p1nets = "3,2,5,3,1,2,2,4,4,3,4,3,5,2,5,3,3,5||26||33||59"; p1nh = "NM||24||3"; p1score = "5,4,6,5,3,4,3,6,5,4,5,4,6,3,6,4,4,6||41||42||83||59"; p1spoints = "3,4,2,3,4,4,3,3,2,3,2,3,1,3,2,2,3,2||28||21||49"; p2nets = "2,4,5,3,2,4,3,4,4,4,3,3,5,2,5,3,4,4||31||33||64"; p2nh = "KS||20||4"; p2score = "4,5,6,4,3,6,4,5,5,5,4,4,6,3,6,4,5,5||42||42||84||64"; p2spoints = "4,2,2,3,3,2,2,3,2,2,3,3,1,3,2,2,2,3||23||21||44"; p3nets = "3,4,5,5,3,5,4,5,5,5,4,5,5,4,5,3,5,6||39||42||81"; p3nh = "AK||18||5"; p3score = "4,5,6,6,4,6,5,6,6,6,5,6,6,5,6,4,6,7||48||51||99||81"; p3spoints = "3,2,2,1,2,1,1,2,1,1,2,1,1,1,2,2,1,1||15||12||27"; p4nets = "2,4,5,4,3,3,3,5,5,4,3,5,4,3,5,4,5,6||34||39||73"; p4nh = "KR||20||6"; p4score = "4,5,6,5,4,5,4,6,6,5,4,6,5,4,6,5,6,7||45||48||93||73"; p4spoints = "4,2,2,2,2,3,2,2,1,2,3,1,2,2,2,1,1,1||20||15||35"; }, ) the way i want normalscoringarray....( id = 1, p1nets = "3,3,4,4,2,2,2,4,3,3,4,2,5,3,5,4,3,4||27||33||60", p1nh = "BP||14||1", p1score = "4,4,5,5,3,3,3,5,4,4,5,3,5,3,6,4,4,5||36||39||75||61", p1spoints = "3,3,3,2,3,4,3,3,3,3,2,4,1,2,2,1,3,3||27||21||48", p2nets = "4,3,4,4,3,4,2,5,4,4,3,3,4,3,6,3,4,4||33||34||67", p2nh = "DG||18||2", p2score = "5,4,5,5,4,5,3,6,5,5,4,4,5,4,7,4,5,5||42||43||85||67", p2spoints = "2,3,3,2,2,2,3,2,2,2,3,3,2,2,1,2,2,3||21||20||41", p3nets = "4,3,6,4,2,3,2,4,5,4,3,5,6,3,4,3,5,6||33||39||72", p3nh = "NM||24||3", p3score = "6,5,7,6,4,5,3,6,6,5,5,6,7,4,5,4,6,7||48||49||97||73", p3spoints = "2,3,1,2,3,3,3,3,1,2,3,1,0,2,3,2,1,1||21||15||36", p4nets = "3,4,5,4,2,3,3,5,3,4,3,5,4,3,5,3,4,6||32||37||69"; p4nh = "KS||20||4"; p4score = "5,5,6,5,3,5,4,6,4,5,5,6,5,4,6,4,5,7||43||47||90||70", p4spoints = "3,2,2,2,3,3,2,2,3,2,3,1,2,2,2,2,2,1||22||17||39", )
NSMutableArray *remainingArray = [[NSMutableArray alloc]init]; for (int i=0; i<[normalscoringarray count]; i++) { NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:[normalscoringarray objectAtIndex:i]]; //each time alloc and init with contents of normalscoringarray //Remove values for keys [mutableDictionary removeObjectForKey:#"cn"]; [mutableDictionary removeObjectForKey:#"gdate"]; [mutableDictionary removeObjectForKey:#"gid"]; [remainingArray addObject:mutableDictionary]; //add mutableDictionary to remainingArray [mutableDictionary release]; //free mutableDictionary } //finally print contents of remainingArray NSLog(#"normalscoringarray:\n%#",[remainingArray description]); Hope this would be help you!
for (int i=0; i<[normalscoringarray count]; i++) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:[normalscoringarray objectAtIndex:i]]; [dictionary removeObjectForKey:#"cn"]; [dictionary removeObjectForKey:#"gdate"]; [dictionary removeObjectForKey:#"gid"]; [normalscoringarray replaceObjectAtIndex:i withObject:dictionary]; }
NSDICTIONARY with multiple keys
I have a NSDICTIONARY : ( { Edad = 11; Genero = M; IdArea = 1; IdEmpleado = 11; Nombre = Manuel; }, { Edad = 30; Genero = M; IdArea = 2; IdEmpleado = 2; Nombre = Luis; } ) How I can do to separate the data in quotes?, To find the values for keys. { Edad = 11; Genero = M; IdArea = 1; IdEmpleado = 11; Nombre = Manuel; } objectforkey #"genero" = M thanks for the help
From the data structure you posted :- It is array which contains number of dictionaries. Now for extracting values for particular key you need to do is:- Suppose DataArray is the array which holds your data. Now for (int i=0; i<[self.DataArray count]; i++) { NSDictionary *dict=[self.DataArray objectAtIndex:i]; NSString *genreString=[dict objectForKey:#"genero"]; }