Objective-C:NSDictionary和循环内部NSDictionaries

时间:2011-09-20 21:44:59

标签: iphone objective-c

当我从服务器解析JSON响应时,我得到以下NSDictionary:

(
     {
        fromUname = Ben;
        id = ci2n9awef7tm7e142sx;
        message = hi;
        read = 1;
        subject = hi;
        time = 1316513972;
        toUname = Jill;
    },
    {
        fromUname = Eamorr;
        id = asdf98s14u7tm7e142sx;
        message = asdf;
        read = 0;
        subject = asdf;
        time = 1316513322;
        toUname = Jack;
    }
)

我真的很难提取这两个科目。

这是我编码的软件(不完整......):

    ...
    SBJsonParser *parser=[[SBJsonParser alloc]init];
    NSDictionary *obj=[parser objectWithString:[request responseString] error:nil];
    NSLog(@"%@",obj);
    NSLog(@"%d",[obj count]);
    for(int i=0;i<[obj count];i++){
        NSDictionary *message=[obj objectForKey:];
        NSLog(@"%@",[message objectForKey:@"subject"]);    //I'm stuck...
    }
    ...

有人能给我一些提取主题的有效方法吗?

非常感谢,

1 个答案:

答案 0 :(得分:2)

它实际上是NSDictionary的NSArray。所以要获取信息,循环遍历数组并获取字典:

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *obj = [parser objectWithString:[request responseString] error:nil];
NSLog(@"%@ : %d",obj, [obj count]);

for (NSDictionary *dict in obj) {
    NSLog(@"%@", [dict objectForKey:@"subject"]);
}