0

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

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

如何解析字典以获取个人姓名、年龄和 ID?谢谢。

4

3 回答 3

2
NSString *name = dictionary[@"data"][@"start"][@"name"];
于 2013-11-04T17:03:54.890 回答
0

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

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

比上一个答案更长,但您知道自己在做什么,而且您不会用 C 编写代码。

顺便说一句,使用其他语法:

NSString *name = dictionary[@"data"][@"start"][0][@"name"];
NSLog(@"%@", name);
于 2013-11-04T17:31:45.887 回答
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);
}
于 2013-11-05T06:55:44.223 回答