Dictionary Value retrieve - iphone
In my application ,the dictionary value contains following content.How to retrieve the each and store it new array
contryArray(
{
checka0 = Thailand;
checka1 = Brazil;
checka10 = Marocco;
checka11 = Thailand;
checka12 = Jordan;
checka13 = Colombia;
checka14 = Kuwait;
checka3 = Mexico;
checka4 = "Saoudi Arabia";
checka5 = Chili;
checka7 = Australia;
checka8 = Malta;
checka9 = "South Africa";
checkb0 = havana;
checkb1 = Santos;
checkb10 = Casablanca;
checkb11 = Bangkok;
checkb12 = Aqaba;
checkb13 = Havana;
checkb14 = Shuwaikh;
checkb3 = Veracruz;
checkb4 = Jeddah;
checkb5 = "San Antonio";
checkb7 = Maersk;
checkb8 = Maraxklokk;
checkb9 = Durban;
checkc0 = 1;
checkc1 = "0.7";
checkc10 = 1;
checkc11 = "1.2";
checkc12 = 1;
checkc13 = "1.4";
checkc14 = 1;
checkc3 = "0.9";
checkc4 = "0.8";
checkc5 = "0.9";
checkc7 = "2.7";
checkc8 = "0.8";
checkc9 = "0.9";
}
For ex: checka0-checka14 in one array, Here the problem is checka2 and checka6 is not available, Im new bee in xcode,Please help me to retrieve
Your question is not clear. Possibly you need to get all keys in your dictionary and separating each item with specific word in it(checka/checkb/checkc).
If that is the case then,
You will get all the keys using:
NSArray *dictKeys = [yourDictionary allKeys];
You can implement this in multiple ways, one of them:
For storing it in same array:
for (NSString *key in [yourDictionary allKeys])
{
[yourArray addObject:[yourDictionary objectForKey:key]];
}
For storing it in seperate arrays:
for (NSString *key in [yourDictionary allKeys])
{
if([key rangeOfString:#"checka"].location != NSNotFound)
{
//add checka to first array
[yourFirstArray addObject:[yourDictionary objectForKey:key]];
}
else if([key rangeOfString:#"checkb"].location != NSNotFound)
{
//add checkb to second array
[yourSecondArray addObject:[yourDictionary objectForKey:key]];
}
...
}
if contryArray is your dictionary then access it as follow
[contryArray valueForKey:#"checka0"];
and go on adding this values to another array
You can use the following code:
- (void)filterDictionary:(NSDictionary *)dict
{
NSArray *allKeys = [dict allKeys];
NSMutableArray *allValues = [[NSMutableArray alloc]init];
for(id key in allKeys)
{
id value = [dict valueForKey:key];
[allValues addObject:[value stringValue]];
}
}
Related
Merge two NSDIctionay in one NSDictonary
I want to merge two NSDictionary in one NSDictionary. my first DIctionary BreakFast Dates Dic { "2012-07-24" = "27"; "2012-08-03" = "98"; "2012-08-06" = "4203"; "2012-08-23" = "0"; "2012-08-24" = "0"; "2012-09-11" = "36"; "2012-09-19" = "450"; "2012-09-24" = "36"; Goals = 3; } my second Dictonary Dinner Dates Dic { "2012-08-03" = "100"; "2012-08-27" = "0"; "2012-09-19" = "270"; Goals = 4; } i want to merge both dictionary into one. like if in breakfast i have "2012-09-19" and same date i have in dinner. i want to separate both values by comma (,) . my resultant dictionary should be like this. combine { "2012-07-24" = "27,0"; "2012-08-03" = "98,100"; "2012-08-06" = "4203,0"; "2012-08-23" = "0,0"; "2012-08-24" = "0,0"; "2012-09-11" = "36,0"; "2012-09-19" = "450,270"; "2012-09-24" = "36,0"; Goals = 3,4; }
NSMutableDictionary *combined = [objectDelegate.dataBreakFastSummary mutableCopy]; for(NSString *key in [combined allKeys]) { NSString *breakfastDate = [objectDelegate.dataBreakFastSummary valueForKey:key]; NSString *dinnerDate = [objectDelegate.dataDinnerSummary objectForKey:key]; if(dinnerDate){ [combined setObject:[NSString stringWithFormat:#"%#,%#",breakfastDate, dinnerDate] forKey:key]; } else { [combined setObject:[NSString stringWithFormat:#"%#,0",breakfastDate] forKey:key]; } } NSLog(#"Combine %#",combined);
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]; }
sorting array of nested NSDictionary containing an array
Im quite new to iOS and objective.. heres my question.. if my array looks like this: myArray = { parentdict = { childdict = { aname = "Aname"; bname - "Bname"; cname = "Cname"; }; childarray = { { counter = "1"; close = "25236"; }, { counter = "2"; close = "12458"; }; }; }; }, { parentdict = { childdict = { aname = "Aname"; bname - "Bname"; cname = "Cname"; }; childarray = { { counter = "1"; close = "28556"; }, { counter = "2"; close = "12118"; }; }; }; }, { parentdict = { childdict = { aname = "Aname"; bname - "Bname"; cname = "Cname"; }; childarray = { { counter = "1"; close = "24356"; }, { counter = "2"; close = "155628"; }; }; }; }; basically its an array of nested dictionary and inside one of the dictionary contains an array of dictionary (childarray) if i want to sort myArray by #"close" of array index 1, which is the one next to counter 2, exactly how should i do this?..(perhaps i should use NSSortDescriptor?) thanks for the reply
You have presented your structure in the JSON format. I assume it is for sake of presenting it to the audience here. If you are actually starting with JSON string, you will have to convert it to nested NSArrayies and NSDictionaryies using some third-party libs or iOS 5 built-in classes.. Assuming you already have your top level NSArray* myArray, give it a try to the following code: NSArray* myArray = // ... this is your array NSArray* sorted_array = [myArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSDictionary* left = obj1; NSDictionary* right = obj2; NSDictionary* left_parent_dict = [left objectForKey: #"parentdict"]; NSDictionary* right_parent_dict = [right objectForKey: #"parentdict"]; NSArray* left_child_array = [left_parent_dict objectForKey: #"childarray"]; NSArray* right_child_array = [right_parent_dict objectForKey: #"childarray"]; NSDictionary* left_child_first = [left_child_array objectAtIndex: 1]; NSDictionary* right_child_first = [right_child_array objectAtIndex: 1]; NSString* left_close = [left_child_first objectForKey: #"close"]; NSString* right_close = [right_child_first objectForKey: #"close"]; NSNumber* left_val = [NSNumber numberWithInt: [left_close intValue]]; NSNumber* right_val = [NSNumber numberWithInt: [right_close intValue]]; return [left_val compare: right_val]; } ]; You will need, of course, to add some checks which I omitted for simplicity. If you want to get descending order modify the last statement to: return [right_val compare: left_val]; I would also suggest you considering SQL database for complex data structures as pointed out by Niko.
Sorting an array can be quite complex, especially when the array contains nested objects. May be you should create an SQLite database
You should use iOS 5.0's built in JSON API. [Here's a link to a great tutorial.] (http://www.raywenderlich.com/5492/working-with-json-in-ios-5). JSON is really easy to work with. And that site also has great tutorials on Core Data (iOS's SQLite API) here. There are two more parts to that tutorial as well.
Try this [array sortUsingComparator:(NSComparator)^(id obj1, id obj2){ int firstValue = [[obj1 objectForKey:#"someKey"] intValue]; int secondValue = [[obj2 objectForKey:#"someKey"] intValue]; int valueDiff = firstValue - secondValue; return (valueDiff == 0) ? NSOrderedSame : (valueDiff < 0) ? NSOrderedAscending : NSOrderedDescending; }];
How to store values of JSON in ARRAY/ String
I have the following JSON value: -( { Key = IsEmail; Value = 1; }, { Key = TrackingInterval; Value = 20; }, { Key = IsBackup; Value = 1; }, { Key = WipeOnRestore; Value = 1; } ) How might I go about parsing this object into an array or string? - i.e. eack key values to be stored in an array and each Value to be stored in another array. Please help me out with this. Thanks :)
This approach uses the json-framework. I've shortened your example: NSString *jsonString = #"[{\"Key\":\"IsEmail\",\"Value\":\"1\"},{\"Key\":\"TrackingInterval\",\"Value\":\"20\"},{\"Key\":\"IsBackup\",\"Value\":\"1\"}]"; NSMutableArray *keys = [NSMutableArray array]; NSMutableArray *values = [NSMutableArray array]; NSArray *json = [jsonString JSONValue]; for (NSDictionary *pair in json) { [keys addObject:[pair objectForKey:#"Key"]]; [values addObject:[pair objectForKey:#"Value"]]; } NSLog(#"%#", keys); NSLog(#"%#", values); Output: 2011-05-18 14:23:55.698 [36736:207] ( IsEmail, TrackingInterval, IsBackup ) 2011-05-18 14:23:55.700 [36736:207] ( 1, 20, 1 )
Refere http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary/ http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/ http://mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api/ http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c
Your data is not vald json, You may want to structure it more like this: var theObj = { IsEmail: 1, TrackingInterval: 20, IsBackup: 1, WipeOnRestore: 1 }; Then you could populate your key and value arrays something like this: var keys = new Array(); var values = new Array(); for (prop in theObj) { keys.push(prop); values.push(theObj[prop]); }
if the JSON is in below format, responseString=[ { Key = IsEmail; Value = 1; }, { Key = TrackingInterval; Value = 20; }, { Key = IsBackup; Value = 1; }, { Key = WipeOnRestore; Value = 1; }] then, NSArray *resultArray=[responseSrting JSONValue]; NSMuatbleArray *keyArray=[[NSMutableArray alloc] init]; NSMutableArray *valueArray=[[NSMutableArray alloc] init]; for(NSDictionary *dict in resultsArray){ [keyArray addObject:[dict objectForKey:#"Key"]]; [valueArray addObject:[dict objectForKey:#"Value"]]; } then, all your keys are stored in keyArray and all your values are stored in valueArray
Trouble reading JSON object using Obj-C
I am trying to read the following json object using the json-framework and obj-C { Sections = { Now = "Wednesday 9 February 2011 02:40"; Section = ( { Article = ( { Exceprt = "text here"; ID = 49011; Title = "text here"; Type = Politics; audioCounter = 0; commentsCounter = 0; hasMore = false; important = False; likesCounter = 0; photoCounter = 0; time = "21:12"; timeStamp = "2/8/2011 9:14:16 PM"; timeStatus = True; videoCounter = 0; viewsCounter = 0; }, { Exceprt = "text here"; ID = 49010; Title = "text here"; Type = Politics; audioCounter = 0; commentsCounter = 0; hasMore = false; important = True; likesCounter = 0; photoCounter = 0; time = "20:45"; timeStamp = "2/8/2011 9:10:59 PM"; timeStatus = True; videoCounter = 0; viewsCounter = 0; }, { Exceprt = "text here"; ID = 49008; Title = "text here"; Type = Politics; audioCounter = 0; commentsCounter = 0; hasMore = false; important = False; likesCounter = 0; photoCounter = 0; time = "20:28"; timeStamp = "2/8/2011 9:09:44 PM"; timeStatus = True; videoCounter = 0; viewsCounter = 0; } ); ID = 22; Name = "EN Live"; totalNews = 3416; } ); }; } My intent is to have a list of the articles (list of dictionaries) so that I can later access them easily. I have been stuck a while on this and my code is giving me an error about calling a non existent method for NSArray which has led me to suspect that I am misunderstanding the json object. I am totally new to this and any help is greatly appreciated. Here's my code: NSDictionary *results = [jsonString JSONValue]; NSDictionary *Articles = [[results objectForKey:#"Sections"] objectForKey:#"Section"]; NSArray *ListOfArticles = [Articles objectForKey:#"Article"]; for (NSDictionary *article in ListOfArticles) { NSString *title = [article objectForKey:#"Title"]; NSLog(title); } Thanks !
First of all, those aren’t valid JSON data. Names (in name/value pairs) are strings and must be quoted. String values must always be quoted. Boolean values must be either true or false (lowercase). Check http://json.org/ and http://www.ietf.org/rfc/rfc4627.txt?number=4627 and http://jsonlint.com Here’s the structure of your data: The top level value is an object (dictionary) This object has a name (key) called Sections whose value is itself another object (dictionary) Sections has a name (key) called Section whose value is an array Each element in the Section array is an object (dictionary) Each element in the Section array has a name (key) called Article whose value is an array, as well as other names (keys): ID, title, totalNews Each element in the Article array is an object If your JSON data were valid, you could parse them as follows: // 1. NSDictionary *results = [jsonString JSONValue]; // 2. NSDictionary *sections = [results objectForKey:#"Sections"]; // 3. NSArray *sectionsArray = [sections objectForKey:#"Section"]; // 4. for (NSDictionary *section in sectionsArray) { // 5. NSLog(#"Section ID = %#", [section objectForKey:#"ID"]; NSLog(#"Section Title = %#", [section objectForKey:#"Title"]; NSArray *articles = [section objectForKey:#"Article"]; // 6. for (NSDictionary *article in articles) { NSLog(#"Article ID = %#", [article objectForKey:#"ID"]; NSLog(#"Article Title = %#", [article objectForKey:#"Title"]; // … } }
Your JSON framework is probably parsing out an NSDictionary where you're expecting an NSArray. It'll let you assign an NSDictionary to an NSArray, but then you'll get a runtime exception when you attempt to call a method on your "array". Judging by the JSON you posted (which isn't correct JSON), this is what I would have my parsing code look like. The names of the NSDictionaries and NSArrays are simply named after the JSON attributes they represent. NSDictionary* results = [jsonString JSONValue]; NSDictionary* sections = [results valueForKey:#"Sections"]; NSArray* section = [sections valueForKey:#"Section"]; NSArray article = [[section objectAtIndex:0] valueForKey:#"Article"]; for (NSDictionary* anArticle in article) { NSLog(#"%#", [anArticle valueForKey:#"Title"]); }