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];
精彩评论