views:

266

answers:

1

Hi I have a json string converted unsing the JSON framework into a dictionary and I need to extract its content. How could I iterate to the nested dictionaries? I have already this code that allows me to see the dictionary:

NSDictionary *results = [responseString JSONValue]; 
NSMutableArray *catArray = [NSMutableArray array];

for (id key in results) {
    NSLog(@"key: %@, value: %@", key, [results objectForKey:key]);
    [catArray addObject:key];
    NSString *cat = key;
}

Could someone provide a sample on how to get to all the levels of the dic not using the name of the keys?

The result structure of the dic is like this: http://www.freeimagehosting.net/uploads/e7c020d697.png

alt text

thanks

ldj

A: 

Try



for ((id) key in [results allKeys]) {
    NSLog(@"key: %@, value: %@", key, [results objectForKey:key]);
    [catArray addObject:key];
    NSString *cat = key;
}

Hope that helped.

EDIT: i'm not sure if i understand your structure correct, but i'm asuming you have some values in your dictionairy which are again dictionairies. if you want to interate through a dictionairy which is located inside another one you could do it like this:


for ((id) key in [results allKeys]) {
    id value = [results objectForKey:key];
    if ([value isKindOfClass:[NSDictionary class]]) {
        NSDictionairy* newDict = (NSDictionairy*)value;
        for ((id) subKey in [newDict allKeys]) {
            ...
        }
    }
}

Of couse if you know the specific key to your desired dictionairy, you can check for that instead of checking if the value is a dict. But maybe it's not a bad idea to check that in both cases...

Tobi
TobiThanks, Yes this code does the same I already have. I am more interested in an example that shows me how I could get all the keys under Arts (Antiques, Glassware) and with those get the entry for business_name under properties. Can you edit your code snippet to serve this objective?Thanks
ldj
hi, ive edited my answer above, is that that what you ment?
Tobi