簡體   English   中英

解析NSDictionary

[英]Parse a NSDictionary

我有一個NSDictionary看起來像這樣:

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

如何解析字典以獲取個人姓名,年齡和身份? 謝謝。

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

我添加了另一個答案,因為我討厭新的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);

你為什么不試試:

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