如何访问 NSDictionary 中"deep"的数据?



>我从Web服务获得JSON响应,如下所示到NSDictionary

NSDictionary *fetchAllCollectionsJSONResponse = [NSJSONSerialization JSONObjectWithData:data
                                                                             options:0
                                                                               error:NULL];

如果我转储 NSDictionary 的输出,它看起来像这样正确

2017-10-06 10:11:46.097698+0800 NWMobileTill[396:33294] +[ShopifyWebServices fetchAllCollections]_block_invoke, {
    data =     {
        shop =         {
            collections =             {
                edges =                 (
                                        {
                        cursor = "eyJsYXN0X2lkIjo0NTI4NTY3MTcsImxhc3RfdmFsdWUiOiI0NTI4NTY3MTcifQ==";
                        node =                         {
                            description = "";
                            id = "Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzQ1Mjg1NjcxNw==";
                        };
                    },
                                        {
                        cursor = "eyJsYXN0X2lkIjo0NTI4NTkwODUsImxhc3RfdmFsdWUiOiI0NTI4NTkwODUifQ==";
                        node =                         {
                            description = "Test Collection 1";
                            id = "Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzQ1Mjg1OTA4NQ==";
                        };
                    },
                                        {
                        cursor = "eyJsYXN0X2lkIjo0NTU0OTMwMDUsImxhc3RfdmFsdWUiOiI0NTU0OTMwMDUifQ==";
                        node =                         {
                            description = Sovrum;
                            id = "Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzQ1NTQ5MzAwNQ==";
                        };
                    },
                                        {
                        cursor = "eyJsYXN0X2lkIjo0NTU0OTMzODksImxhc3RfdmFsdWUiOiI0NTU0OTMzODkifQ==";
                        node =                         {
                            description = Badrum;
                            id = "Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzQ1NTQ5MzM4OQ==";
                        };
                    }
                );
                pageInfo =                 {
                    hasNextPage = 0;
                };
            };
        };
    };
}

我需要访问此结构深处的"description"属性,但我不知道该怎么做。

我尝试了以下方法,但它崩溃了

for (NSDictionary *dictionary in fetchAllCollectionsJSONResponse) {
    NSLog(@"jongel %@", [dictionary objectForKey:@"data"]);
}

>@Bilal的答案是正确的。这可能更容易阅读:

NSArray *edges = fetchAllCollectionsJSONResponse[@"data"][@"shop"][@"collections"][@"edges"];
for (NSDictionary *edge in edges) {
    NSString *description = edge[@"node"][@"description"];
    NSLog(@"description = %@", description);
}

fetchAllCollectionsJSONResponseDictionary而不是Array。试试这个。

NSDictionary *fetchAllCollectionsJSONResponse = nil;
NSDictionary *data = fetchAllCollectionsJSONResponse[@"data"];
NSDictionary *shop = fetchAllCollectionsJSONResponse[@"shop"];
NSDictionary *collections = fetchAllCollectionsJSONResponse[@"collections"];
NSArray *edges = fetchAllCollectionsJSONResponse[@"edges"];
// Or a shorter version 
// NSArray *edges = fetchAllCollectionsJSONResponse[@"data"][@"shop"][@"collections"][@"edges"];
for (NSDictionary *edge in edges) {
    NSString *cursor = edge[@"cursor"];
    NSDictionary *node = edge[@"node"];
}

最新更新