简体   繁体   English

Objective-C:NSDictionary和循环内部NSDictionaries

[英]Objective-C: NSDictionary and looping through inner NSDictionaries

I get the following NSDictionary when I parse a JSON response from my server: 当我从服务器解析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;
    }
)

I'm really struggling to extract the two subjects. 我真的很难提取这两个科目。

Here's what I've coded sofar (incomplete...): 这是我编码的软件(不完整......):

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

Can anyone give me some efficient way of extracting the subjects? 谁能给我一些提取主题的有效方法?

Many thanks in advance, 提前谢谢了,

Its actually an NSArray of NSDictionaries. 它实际上是NSDictionary的NSArray。 So to get the information, loop through the array and get the dictionary: 因此,要获取信息,遍历数组并获取字典:

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"]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM