NSDICTIONARY with multiple keys - iphone

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"];
}

Related

Sorting NSDictionary which contains another dictionary

I face the problem, when i sort the dictionary, i can't get output from this.
here i show my NSMutableDictionary which contains.
ReasonMst is my NSMutableDictionary.
Printing description of ReasonMst:
{
1 = {
nReasonNo = 1;
vReasonCode = 1;
vReasonDesc = "Office Purpose";
vReasonType = O;
};
10 = {
nReasonNo = 10;
vReasonCode = 10;
vReasonDesc = Transit;
vReasonType = O;
};
2 = {
nReasonNo = 2;
vReasonCode = 2;
vReasonDesc = Personal;
vReasonType = O;
};
3 = {
nReasonNo = 3;
vReasonCode = 3;
vReasonDesc = Specific;
vReasonType = O;
};
4 = {
nReasonNo = 4;
vReasonCode = 4;
vReasonDesc = "Meeting";
vReasonType = I;
};
5 = {
nReasonNo = 5;
vReasonCode = 5;
vReasonDesc = "Meeting arrange";
vReasonType = I;
};
6 = {
nReasonNo = 6;
vReasonCode = 6;
vReasonDesc = "NATIONAL CONFERENCE";
vReasonType = I;
};
7 = {
nReasonNo = 7;
vReasonCode = 7;
vReasonDesc = Other;
vReasonType = O;
};
8 = {
nReasonNo = 8;
vReasonCode = 8;
vReasonDesc = "Briefing Meeting";
vReasonType = I;
};
9 = {
nReasonNo = 9;
vReasonCode = 9;
vReasonDesc = "Meeting At HO";
vReasonType = I;
};
KeyList = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
""
);
}
i want to sort this NSMutableDictionary to vReasonDesc wise. Can any one tell me how can i do this easiest way ?
Suppose ReasonMst is your NSMutableDictionary, do this
NSArray *key=[[NSArray alloc]initWithArray:[ReasonMst objectForKey:#"KeyList"]];
NSMutableArray *vReasonDesc=[[NSMutableArray alloc]init];
NSMutableArray *sortedKeys=[[NSMutableArray alloc]initWithArray:nil];
NSDictionary *tempDict=[[NSDictionary alloc]init];
for (int i=0; i<[key count]; i++) {
[vReasonDesc addObject:[[ReasonMst objectForKey:[key objectAtIndex:i]]objectForKey:#"vReasonDesc"]];
}
[vReasonDesc sortUsingSelector:#selector(caseInsensitiveCompare:)];
for (int i=0; i<[vReasonDesc count]; i++) {
for (int j=0; j<[key count]; j++) {
tempDict=[ReasonMst objectForKey:[key objectAtIndex:j]];
NSLog(#"%# ... %#",[vReasonDesc objectAtIndex:i], tempDict);
if ([[tempDict objectForKey:#"vReasonDesc"]isEqualToString:[vReasonDesc objectAtIndex:i]]) {
[sortedKeys addObject:[tempDict objectForKey:#"vReasonNo"]];
}
}
}
NSLog(#"sdafa %#",sortedKeys);
sortedKeys contain the keys sorted according to your vReasonDesc values... now you can access your ReasonMst according to the keys in sortedKeys which inorder.
for (int i=0; i<[sortedKeys count]; i++) {
NSLog (#"%#",[ReasonMst objectForKey:[sortedKeys objectAtIndex:i]]);
}

How to get data into NSArray from NSDictionary

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.

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

Objective-C -> Remove Last Element In NSDictionary

EDIT:
The Code:
//stores dictionary of questions
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
NSString *json = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *qs = [json objectFromJSONString];
self.questions = qs;
NSLog(#"%#", questions);
[json release];
[self setQuestions];
[load fadeOut:load.view withDuration:0.7 andWait:0];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Start" style:UIBarButtonItemStylePlain target:self action:#selector(start:)];
self.navigationItem.rightBarButtonItem = anotherButton;
}
I have the following items in an NSDictionary:
(
{
max = 120;
min = 30;
question = "Morning Bodyweight (Kg)";
questionId = 1;
questionNumber = 1;
sectionId = 1;
type = TextInput;
},
{
question = "Morning Urine Colour";
questionId = 2;
questionNumber = 2;
sectionId = 1;
type = ImagePicker;
},
{
max = 120;
min = 30;
question = "Evening Bodyweight (Kg)";
questionId = 3;
questionNumber = 3;
sectionId = 1;
type = TextInput;
},
{
question = "Evening Urine Colour";
questionId = 4;
questionNumber = 4;
sectionId = 1;
type = ImagePicker;
},
{
max = 90;
min = 40;
question = "Morning Heart Rate (BPM)";
questionId = 5;
questionNumber = 5;
sectionId = 1;
type = TextInput;
},
{
question = "Time of Month (TOM)";
questionId = 6;
questionNumber = 6;
sectionId = 1;
type = Option;
}
)
I want to remove the last element:
{
question = "Time of Month (TOM)";
questionId = 6;
questionNumber = 6;
sectionId = 1;
type = Option;
}
Is there a pop() equivalent for the NSDictionary? If not how is it possible to remove the last element?
There is no order to dictionaries so there is no 'last object'
However, this might solve your problem, though it might not always remove what you are thinking the 'last object' is:
[dictionaryName removeObjectForKey:[[dictionaryName allKeys] lastObject]];
This looks to be (or could be made to be) an array of dictionaries. If you have these dictionaries as the objects of an NSMutableArray, then you can use – removeLastObject. Otherwise, you're SOL since even NSMutableDictionary has no such method.
There is no last element in a dictionary, as elements in a dictionary are not ordered.
Can you somehow get the element by using the key value? NSDictionaries don't have an ordering, so there's no such thing as removing the "last" element.
I think that's an array of NSDictionaries you got yourself there. In which case it's very easy to do:
NSMutableArray *mArray = [NSMutableArray arrayWithArray:array]; // if not mutable
[mArray removeLastObject];