user2543991
user2543991

Reputation: 625

Parse a NSDictionary

I have a NSDictionary looks like this:

 data = {
    start = {
        name = "abc";
        age = "123";
        id = AA838DDE;
    };
};

how can I parser the dictionary to get individual name, age, and id? Thanks.

Upvotes: 0

Views: 88

Answers (3)

staticVoidMan
staticVoidMan

Reputation: 20234

why don't you simply try:

int arrCount = [[[dictionaryObject valueForKey:@"data"] objectForKey:@"start"] count];

for(int i = 0 ; i < arrCount ; i++)
{
    NSString *strName = [[[[dictionaryObject objectForKey:@"data"]
                                             objectForKey:@"start"]
                                            objectAtIndex:i]
                                             objectForKey:@"name"];

    NSString *strAge = [[[[dictionaryObject objectForKey:@"data"]
                                            objectForKey:@"start"]
                                           objectAtIndex:i]
                                            objectForKey:@"age"];

    NSString *strID = [[[[dictionaryObject objectForKey:@"data"]
                                           objectForKey:@"start"]
                                          objectAtIndex:i]
                                           objectForKey:@"id"];

    NSLog(@"%@ - %@ - %@", strName, strAge, strID);
}

Upvotes: 0

Jordan Montel
Jordan Montel

Reputation: 8247

I add an other answer because I hate the new Objective-C syntax (sorry @Raphael Olivieira)

NSString *name = [[[[dictionary objectForKey:@"data"] objectForKey:@"start"] objectAtIndex:0] objectForKey:@"name"];
NSLog(@"%@", name);

Longer than the previous answer but you know what you do and you don't code in C.

BTW, using the other syntax :

NSString *name = dictionary[@"data"][@"start"][0][@"name"];
NSLog(@"%@", name);

Upvotes: 0

Raphael Oliveira
Raphael Oliveira

Reputation: 7841

NSString *name = dictionary[@"data"][@"start"][@"name"];

Upvotes: 2

Related Questions