如何从NSDictionary的NSArray中获取元素

时间:2014-06-10 17:08:44

标签: nsarray nsdictionary

这是我的NSArray的NSLog

[{"id":16,"venueId":16,"street":"171 - 3401 Dufferin St","city":"Toronto","zipcode":"M6A 2T9","province":"ON","country":"Canada"}]

2 个答案:

答案 0 :(得分:0)

NSDictionary *dict = [myarray objectAtIndex:i] 
//myarray is your array of dictionary
//if the array has just one element like in your example, i will be 0 

NSNumber *venueId = [dict objectForKey:@"venueId"];

答案 1 :(得分:0)

最后我找到了解决方案

NSError *error;
    NSArray* jsonArray = [NSJSONSerialization JSONObjectWithData:[[venue objectForKey:@"address"] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error] ;

    NSDictionary *dict = [jsonArray objectAtIndex:0];
    //myarray is your array of dictionary

    NSString *street = [dict objectForKey:@"street"];
    NSLog(@"street: %@", street);

注意:我使用下面的代码来检查数据类型,以确保它返回有效的类。

if ([[venue objectForKey:@"address"] isKindOfClass:[NSArray class]]) {
        NSLog(@"%@", @"It is NSArray");
    } else if ([[venue objectForKey:@"address"] isKindOfClass:[NSString class]]) {
        NSLog(@"%@", @"It is NSString");
    }