1

我使用一个 API,它为我提供来自网络的数据。数据为 JSON 格式,并存储在NSDictionary. 像这样:

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

Ggb控制台结果: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";
        };
   

如何访问值“1262”和“1993”并将它们放入 aNSArray中以在 a 中使用UITableView

4

2 回答 2

5

首先,SBJSON 的objectWithString:error:方法将根据 JSON 消息中的根对象返回以下内容之一:

  • NSArray
  • NS词典

所以,你不应该总是假设它会返回一个字典,除非你知道 JSON 中将返回什么。

其次,您正在分配一个新的 NSDictionary 对象,然后将解析器的结果分配给您的 dict 变量,从而泄漏先前分配的字典。您不需要这一行:dict = [[NSDictionary alloc]init];.

最后,由于返回的对象字典,您可以像这样从字典中获取所有对象:

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

    //do stuff with feed.
}
于 2010-02-15T15:44:23.203 回答
4
return [dict allKeys];
于 2010-02-15T15:14:17.950 回答