views:

427

answers:

2

Hello,

I use a api wich provides me data from the web. The data is in Json format, and is stored in a NSDictionary. Like this:

SBJSON *parser = [[SBJSON alloc] init];
dict = [[NSDictionary alloc]init];
dict = [parser objectWithString:jsonArray error:nil];

Ggb console result for: po dict

        1262 =             {
            "feed_name" = name1;
            "new_results" = 6;
            "next_update" = "2010-02-16T15:22:11+01:00";
        };
        1993 =             {
            "feed_name" = name2;
            "new_results" = 0;
            "next_update" = "2010-02-16T09:09:00+01:00";
        };

How can i access the values "1262" and "1993" and put them in a NSArray, for use in a UItableView

Many thanks, Regards, Bart

+3  A: 
return [dict allKeys];
KennyTM
+2  A: 

First of all, SBJSON's objectWithString:error: method will return one of the folowing depending on what the root object is in the JSON message:

  • NSArray
  • NSDictionary

So, you shouldn't always assume it'll return you a dictionary unless you know for a fact what will be returned in the JSON.

Secondly, you're allocating a new NSDictionary object, but then assigning the result of the parser to your dict variable, leaking the previously allocated dictionary. You don't need this line: dict = [[NSDictionary alloc]init];.

Finally, since the returned object is a dictionary, you can get al of the objects out of the dictionary like this:

for (NSString *key in [dict allKeys])
{
    NSDictionary *feed = [dict objectForKey:key];

    //do stuff with feed.
}
Jasarien
Thanks for the quick response! it worked out for me! The memory issue is also gone after your suggestion.
Bart Schoon