解析NSDictionary

时间:2013-11-04 17:01:47

标签: ios nsdictionary

我有一个NSDictionary看起来像这样:

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

如何解析字典以获取个人姓名,年龄和身份证?感谢。

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我添加了另一个答案,因为我讨厌新的Objective-C语法(对不起@Raphael Olivieira)

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

比上一个答案更长,但你知道你做了什么,而且你不用C编码。

BTW,使用其他语法:

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

答案 2 :(得分:0)

为什么不试试:

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