Eamorr
Eamorr

Reputation: 10012

Objective-C: NSDictionary and looping through inner NSDictionaries

I get the following NSDictionary when I parse a JSON response from my server:

(
     {
        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,

Upvotes: 0

Views: 449

Answers (1)

max_
max_

Reputation: 24481

Its actually an NSArray of NSDictionaries. 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"]);
}

Upvotes: 2

Related Questions