簡體   English   中英

如何訪問NSDictionary內部的“深度”數據?

[英]How to access data “deep” inside a NSDictionary?

我正在從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);
}

fetchAllCollectionsJSONResponse是一個Dictionary而不是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"];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM