开发者

How to get key value from NSdictonary - iphone sdk

开发者 https://www.devze.com 2022-12-20 00:55 出处:网络
I use an API which provides me data from the web. The data is in JSON format, and is stored in a NSDictionary. Like this:

I use an API which 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_upda开发者_运维技巧te" = "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?


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.
}


return [dict allKeys];
0

精彩评论

暂无评论...
验证码 换一张
取 消