1

I have a view that has tableviewcells on it, loaded with different "key values" as the label. When I tap on one I open another view. However here, I pass the dictionary for just that key, for example I would pass this:

{
    key = Budget;
    value =     {
        "2012 Budget Report" =         {
            active = 0;
            author = "xxxxx xxxxxx";
            date = "October 27, 2012";
            description = "Example";
            dl = "53 downloads";
            email = "xxx@xxxxx.com";
            ext = DOCX;
            fortest = "Tuesday, November 6";
            id = 5;
            subject = budget;
            testdate = "Tuesday, November 6";
            title = "Budget spreadSheet";
        };
        "2005 - 2008 Budget Report" =         {
            active = 0;
            author = "xxxxxxx xxxxx";
            date = "November 3, 2012";
            description = "Example";
            dl = "18 downloads";
            email = "xxxxx@xxxxx.com";
            ext = DOCX;
            title = "Budget report";
        };
    };
}

How do I get each of these values? Thanks.

Please note: the titles in value array are subject to change... More could be added, one could be deleted, so I need a general solution.

1789040
  • 569
  • 2
  • 6
  • 17

3 Answers3

11

Considering the dictionary you passed is saved in iDictionary.

NSDictionary *iDictionary // Input Dictionary;
NSDictionary *theValues = [NSDictionary dictionaryWithDictionary:[iDictionary valueForKey:@"value"]];

for (NSString *aKey in [theValues allKeys]) {
    NSDictionary *aValue = [theValues valueForKey:aKey];
    NSLog(@"Key : %@", aKey);
    NSLog(@"Value : %@", aValue);

    // Extract individual values
    NSLog(@"Author : %@", [aValue objectForKey:@"author"]);

    // If the titles are dynamic
    for (NSString *aSubKey in [aValue allKeys]) {
        NSString *aSubValue = [aValue objectForKey:aSubKey];

        NSLog(@"SubKey : %@, SubValue = %@", aSubKey, aSubValue);
    }
}
Roshit
  • 1,589
  • 1
  • 15
  • 37
  • 1
    should also check the answer on this page for an easy to understand implementation. http://stackoverflow.com/questions/2143372/for-each-loop-in-objective-c-for-accessing-nsmutable-dictionary – impiyush Dec 20 '14 at 00:19
1
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

NSArray *arrBudget= [jsonDictionary objectForKey:@"Budget"];

So here arrBudget will contain All the values And you can Pass the array to detail view.

iDev
  • 23,310
  • 7
  • 60
  • 85
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40
0

Another approach if keys and objects are useful in the "foreach" logic :

NSDictionary *dict = @{
    @"key1": @"value1",
    @"key2": @"value2",
    @"key3": @"value3",
};  
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSLog(@"Your key : %@", key);
    NSLog(@"Your value : %@", [obj description]);
    // do something...
}];
Dulgan
  • 6,674
  • 3
  • 41
  • 46