简体   繁体   中英

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.

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

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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