views:

2469

answers:

3

I have a Dictionary of Dictionaries which is being returned to me in JSON format

{
    "neverstart": {
     "color": 0, 
     "count": 0, 
     "uid": 32387, 
     "id": 73129, 
     "name": "neverstart"
    }, 
    "dev": {
     "color": 0, 
     "count": 1, 
     "uid": 32387, 
     "id": 72778, 
     "name": "dev"
    }, 
    "iphone": {
     "color": 0, 
     "count": 1, 
     "uid": 32387, 
     "id": 72777, 
     "name": "iphone"
    }
}

I also have an NSArray containing the id's required for an item. e.g. [72777, 73129]

What I need to be able to do is get a dictionary of id => name for the items in the array. I know this is possible by iterating through the array, and then looping through all the values in the Dictionary and checking values, but it seems like it there should be a less longwinded method of doing this.

Excuse my ignorance as I am just trying to find my way around the iPhone SDK and learning Objective C and Cocoa.

+2  A: 

Have you tried this NSDictionary method:

+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
drewh
I was not asking how to build a dictionary, rather the best way to filter an existing dictionary.
Xetius
A: 

Drew's got the answer... I found that the GCC manual for the NSDictionary was helpful in a bare-bones way the other day when I had a similar question.

http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSDictionary.html

Jim Carroll
Considering he's programming on the iPhone, consulting Apple's documentation is a much better idea... http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/
Quinn Taylor
+5  A: 

First off, since you're using JSON, I'm hoping you've already found BSJSONAdditions and/or json-framework, both of them open-source projects for parsing JSON into native Cocoa structures for you. This blog post gives an idea of how to use the latter to get an NSDictionary from a JSON string.

The problem then becomes one of finding the matching values in the dictionary. I'm not aware of a single method that does what you're looking for — the Cocoa frameworks are quite powerful, but are designed to be very generic and flexible. However, it shouldn't be too hard to put together in not too many lines... (Since you're programming on iPhone, I'll use fast enumeration to make the code cleaner.)

NSDictionary* jsonDictionary = ...
NSDictionary* innerDictionary;
NSArray* requiredIDs = ...
NSMutableDictionary* matches = [NSMutableDictionary dictionary];
for (id key in jsonDictionary) {
    innerDictionary = [jsonDictionary objectForKey:key];
    if ([requiredIDs containsObject:[innerDictionary objectForKey:@"id"]])
        [matches setObject:[innerDictionary objectForKey:@"name"]
                    forKey:[innerDictionary objectForKey:@"id"]];
}

This code may have typos, but the concepts should be sound. Also note that the call to [NSMutableDictionary dictionary] will return an autoreleased object.

Quinn Taylor