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
Related
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]]);
}
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 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.
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.
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