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]]);
}
Related
i am new to ios development..
i have problem in getting arrays from complex json response.
so anyone have idea about it than please help me...
my json response is as below
and i want to get array of subcat_id and subcat_name
(
{
0 = {
"subcat_id" = 2;
"subcat_name" = Restaurants;
};
1 = {
"subcat_id" = 3;
"subcat_name" = "Bar & Club";
};
"cat_id" = 1;
"cat_name" = "Dining & Nightlife";
},
{
0 = {
"subcat_id" = 5;
"subcat_name" = Massage;
};
1 = {
"subcat_id" = 6;
"subcat_name" = Facial;
};
2 = {
"subcat_id" = 7;
"subcat_name" = "Manicure / Pedicure";
};
3 = {
"subcat_id" = 8;
"subcat_name" = Tanning;
};
4 = {
"subcat_id" = 9;
"subcat_name" = "Hair Salon";
};
5 = {
"subcat_id" = 10;
"subcat_name" = "Hair Removal";
};
6 = {
"subcat_id" = 11;
"subcat_name" = Spa;
};
7 = {
"subcat_id" = 12;
"subcat_name" = "Teeth Whitening";
};
8 = {
"subcat_id" = 13;
"subcat_name" = "Eye & Vision";
};
9 = {
"subcat_id" = 15;
"subcat_name" = "Cosmetic & Beauty Treatments";
};
"cat_id" = 4;
"cat_name" = "Health & Beauty";
},
{
0 = {
"subcat_id" = 17;
"subcat_name" = Pilates;
};
1 = {
"subcat_id" = 18;
"subcat_name" = Yoga;
};
2 = {
"subcat_id" = 19;
"subcat_name" = Gym;
};
3 = {
"subcat_id" = 20;
"subcat_name" = "Boot Camp";
};
"cat_id" = 16;
"cat_name" = Fitness;
},
{
0 = {
"subcat_id" = 22;
"subcat_name" = "Men's Clothing";
};
1 = {
"subcat_id" = 23;
"subcat_name" = "Women's Clothing";
};
2 = {
"subcat_id" = 24;
"subcat_name" = "Food & Grocery";
};
3 = {
"subcat_id" = 25;
"subcat_name" = "Wine & Liquor";
};
4 = {
"subcat_id" = 26;
"subcat_name" = "Home Services";
};
5 = {
"subcat_id" = 27;
"subcat_name" = "Rental Car/ Car Wash/ Car Repair";
};
6 = {
"subcat_id" = 53;
"subcat_name" = "Product / Service Discounts";
};
"cat_id" = 21;
"cat_name" = "Retail & Services";
},
{
0 = {
"subcat_id" = 29;
"subcat_name" = Museums;
};
1 = {
"subcat_id" = 30;
"subcat_name" = "Wine Tasting";
};
10 = {
"subcat_id" = 42;
"subcat_name" = "Dance Classes";
};
11 = {
"subcat_id" = 50;
"subcat_name" = "Family Fun";
};
2 = {
"subcat_id" = 31;
"subcat_name" = "City Tours";
};
3 = {
"subcat_id" = 32;
"subcat_name" = "Comedy Clubs";
};
4 = {
"subcat_id" = 33;
"subcat_name" = Theater;
};
5 = {
"subcat_id" = 34;
"subcat_name" = Concerts;
};
6 = {
"subcat_id" = 35;
"subcat_name" = "Education & Personal Development";
};
7 = {
"subcat_id" = 36;
"subcat_name" = Golf;
};
8 = {
"subcat_id" = 38;
"subcat_name" = Bowling;
};
9 = {
"subcat_id" = 39;
"subcat_name" = "Sporting Events";
};
"cat_id" = 28;
"cat_name" = "Activities & Adventures";
},
{
0 = {
"subcat_id" = 44;
"subcat_name" = Others;
};
1 = {
"subcat_id" = 56;
"subcat_name" = "Accommodation and Holidays";
};
"cat_id" = 43;
"cat_name" = Others;
},
{
0 = {
"subcat_id" = 46;
"subcat_name" = "Pet Grooming Services";
};
"cat_id" = 45;
"cat_name" = Pets;
},
{
0 = {
"subcat_id" = 52;
"subcat_name" = "Car Servicing / Repairs";
};
"cat_id" = 51;
"cat_name" = "Car Servicing / Repairs";
}
)
What you have posted in your question isn't JSON. It might have been originally transmitted as JSON data. But the post shows a data structure in the property list format. Note that some of the strings are HTML or XML encoded which will cause problems later. You should fix this at the source before transmitting it as JSON.
Now the result you post is an array of dictionaries containing further dictionaries. The outmost array is a list of categories. Each category is given as a dictionary containg both its own name and ID as well as the subcategories. The innermost dictionaries are the subcategories containing their ID and name.
The category dictionaries are somewhat problematic because the mix two things and are a mixture of a dictionary and an array. It's difficult to access the subcategories.
Your code could like like this:
NSArray* categoryList = [someJSONParser parse];
for (NSDictionary* category in categoryList ) {
NSString* categoryName = [category objectForKey: #"cat_name"];
NSNumber* categoryIdObj = [category objectForKey: #"cat_id"];
int categoryId = [categoryIdObj intValue];
int index = 0;
NSDictionary* subcategory = [category objectForKey: [NSString stringWithFormat: #"%d", index] ];
while (subcategory != nil) {
NSString* subcategoryName = [subcategory objectForKey: #"subcat_name"];
NSNumber* subcategoryIdObj = [subcategory objectForKey: #"subcat_id"];
int subcategoryId = [subcategoryIdObj intValue];
index++;
subcategory = [category objectForKey: [NSString stringWithFormat: #"%d", index] ];
}
}
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
I have a NSDictionary like this one.
PhotoData = {
1 = {
"jf_photo_geotag" = "arayat philippines";
"jf_photo_id" = 1;
"jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png";
"jf_user_id" = 1;
comments = {
1 = {
"jf_photo_comment" = "well i like this one. really. comment from janus";
"jf_photo_comment_id" = 1;
"jf_photo_id" = 1;
"jf_user_id" = 1;
"jf_username" = janus;
};
4 = {
"jf_photo_comment" = asasfa;
"jf_photo_comment_id" = 4;
"jf_photo_id" = 1;
"jf_user_id" = 1;
"jf_username" = janus;
};
};
likes = {
2 = {
"jf_like_id" = 2;
"jf_photo_id" = 1;
"jf_user_id" = 1;
"jf_username" = janus;
};
};
"post_date" = "2012-02-23 16:34:14";
};
2 = {
"jf_photo_geotag" = "magalang philippines";
"jf_photo_id" = 2;
"jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage2.png";
"jf_user_id" = 2;
comments = {
2 = {
"jf_photo_comment" = "well this is now a comment from loaclhost! for phot number 2";
"jf_photo_comment_id" = 2;
"jf_photo_id" = 2;
"jf_user_id" = 2;
"jf_username" = fidel;
};
3 = {
"jf_photo_comment" = asdfadf;
"jf_photo_comment_id" = 3;
"jf_photo_id" = 2;
"jf_user_id" = 2;
"jf_username" = fidel;
};
};
likes = {
1 = {
"jf_like_id" = 1;
"jf_photo_id" = 2;
"jf_user_id" = 1;
"jf_username" = janus;
};
};
"post_date" = "2012-02-23 16:34:31";
};
3 = {
"jf_photo_geotag" = "san fdo philippines";
"jf_photo_id" = 3;
"jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png";
"jf_user_id" = 3;
comments = (
);
likes = (
);
"post_date" = "2012-02-24 16:34:47";
};
};
success = 1;
}
basically, this NSDictionary contains some string data like jf_photo_url, jf_photo_id etc. and also it has another dictionary as its object like comments = ()
how can I work with this dictionary to change its structure and produce and maybe create a new Dictionary similar to this one:
PhotoData = {
{
"jf_photo_geotag" = "arayat philippines";
"jf_photo_id" = 1;
"jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png";
"jf_user_id" = 1;
comments = (
{
"jf_photo_comment" = "well i like this one. really. comment from janus";
"jf_photo_comment_id" = 1;
"jf_photo_id" = 1;
"jf_user_id" = 1;
"jf_username" = janus;
},
{
"jf_photo_comment" = asasfa;
"jf_photo_comment_id" = 4;
"jf_photo_id" = 1;
"jf_user_id" = 1;
"jf_username" = janus;
}
};
likes = (
{
"jf_like_id" = 2;
"jf_photo_id" = 1;
"jf_user_id" = 1;
"jf_username" = janus;
}
,"post_date" = "2012-02-23 16:34:14";
},
{
"jf_photo_geotag" = "magalang philippines";
"jf_photo_id" = 2;
"jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage2.png";
"jf_user_id" = 2;
comments =(
{
"jf_photo_comment" = "well this is now a comment from loaclhost! for phot number 2";
"jf_photo_comment_id" = 2;
"jf_photo_id" = 2;
"jf_user_id" = 2;
"jf_username" = fidel;
},
{
"jf_photo_comment" = asdfadf;
"jf_photo_comment_id" = 3;
"jf_photo_id" = 2;
"jf_user_id" = 2;
"jf_username" = fidel;
};
};
likes = (
{
"jf_like_id" = 1;
"jf_photo_id" = 2;
"jf_user_id" = 1;
"jf_username" = janus;
};
},
"post_date" = "2012-02-23 16:34:31";
},
{
"jf_photo_geotag" = "san fdo philippines";
"jf_photo_id" = 3;
"jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png";
"jf_user_id" = 3;
comments = (
);
likes = (
);
"post_date" = "2012-02-24 16:34:47";
};
};
success = 1;
}
notice that the first dictionary has index number in each object. I want to remove them.
my first dictionary is loaded from the server, the second one is from a plist
thanks you guys! I hope you understand my question.
It looks like you want your outer dictionary to be an array instead. Assuming you have this data in an NSDictionary already, you could copy it to an array like so:
NSDictionary* photoDict = some_dict; //your outer dict
NSMutableArray* photoArray = [[[NSMutableArray alloc] init] autorelease];
NSEnumerator *enumerator = [photoDict objectEnumerator];
id value;
while((value = [enumerator nextObject])) {
[photoArray addObject:value];
}
//do something with photoArray
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"];
}
-(IBAction) balancer: (id) sender{
double M[4][4];
M[0][0] = 6.0;
M[0][1] = 0.0;
M[0][2] = -1.0;
M[0][3] = 0.0;
M[1][0] = 12.0;
M[1][1] = 0.0;
M[1][2] = 0.0;
M[1][3] = 2.0;
M[2][0] = 6.0;
M[2][1] = 2.0;
M[2][2] = -2.0;
M[2][3] = 1.0;
M[3][0] = 0.0;
M[3][1] = 0.0;
M[3][2] = 0.0;
M[3][3] = 0.0;
int rowCount = 4;
int columnCount = 4;
int lead = 0;
for (int r = 0; r < rowCount; r++) {
if (lead >= columnCount)
break;
int i = r;
while (M[i][lead] == 0) {
i++;
if (i == rowCount) {
i = r;
lead++;
if (lead == columnCount){
break;
}
}
}
double temp[4] ;
temp[0] = M[r][0];
temp[1] = M[r][1];
temp[2] = M[r][2];
temp[3] = M[r][3];
M[r][0] = M[i][0];
M[r][1] = M[i][1];
M[r][2] = M[i][2];
M[r][3] = M[i][3];
M[i][0] = temp[0];
M[i][1] = temp[1];
M[i][2] = temp[2];
M[i][3] = temp[3];
double lv = M[r][lead];
for (int j = 0; j < columnCount; j++)
M[r][j] = M[r][j] / lv;
for (int f = 0; f < rowCount; f++) {
if (f != r) {
double l = M[f][lead];
for (int j = 0; j < columnCount; j++)
M[f][j] = M[f][j] - l * M[r][j];
}
}
lead++;
}
NSString* myNewString = [NSString stringWithFormat:#"%g",M[0][3]];
label1.text = myNewString;
}
This is returning NaN, while it should be returning .16666667 for M[0][3]. Any suggestions on how to fix this?
you are making a division to obtain the values for M[r][j], you should check that lv ! = 0.0 prior performing the division.
double TOLERANCE = 1.0E-10;
if (fabs(lv) < TOLERANCE){
NSLog(#"Division over ZERO");
exit(1);
}
[edit] Assuming that your logic is correct.