简体   繁体   English

解析NSDictionary

[英]Parse a NSDictionary

I have a NSDictionary looks like this: 我有一个NSDictionary看起来像这样:

 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) 我添加了另一个答案,因为我讨厌新的Objective-C语法(对不起@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. 比之前的答案更长,但你知道你做了什么,你不用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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