访问嵌套的NSDictionary项

时间:2014-09-17 03:32:49

标签: ios objective-c

我收到了来自api电话的NSDictionary:

{
items =     (
            {
        accessInfo =             {
            accessViewStatus = SAMPLE;
            country = US;
            embeddable = 1;
            epub =                 {
                isAvailable = 0;
            };
            pdf =                 {
                isAvailable = 0;
            };
            publicDomain = 0;
            quoteSharingAllowed = 0;
            textToSpeechPermission = ALLOWED;
            viewability = PARTIAL;
            webReaderLink = "http://books.google.com/books/reader?id=LdB2_WzYpKgC&hl=&printsec=frontcover&output=reader&source=gbs_api";
        };
        etag = 1xxAlevFUSc;
        id = "LdB2_WzYpKgC";
        kind = "books#volume";
        saleInfo =             {
            country = US;
            isEbook = 0;
            saleability = "NOT_FOR_SALE";
        };
        searchInfo =             {
            textSnippet = "The saga of their daily exploits won cartoonist Bill Watterson the coveted Reuben Award for "Outstanding Cartoonist of the Year." Something Under the Bed Is Drooling is a jewel.";
        };
        selfLink = "https://www.googleapis.com/books/v1/volumes/LdB2_WzYpKgC";
        volumeInfo =             {
            authors =                 (
                "Bill Watterson"
            );
            averageRating = "4.5";
            canonicalVolumeLink = "http://books.google.com/books/about/Something_Under_the_Bed_Is_Drooling.html?hl=&id=LdB2_WzYpKgC";
            categories =                 (
                Humor
            );
            contentVersion = "0.0.1.0.preview.1";
            description = "\"Be good to yourself: Buy a copy of this Calvin and Hobbes cartoon book. If you don't laugh out loud at every third strip, check your pulse. You may be dead.\" --Phil Musick, Pittsburgh Press Calvin is a rambunctious six-year-old whose manic antics threaten world peace. Hobbes is his stuffed tiger who comes alive when adults aren\"t around. The saga of their daily exploits won cartoonist Bill Watterson the coveted Reuben Award for \"Outstanding Cartoonist of the Year.\" Something Under the Bed Is Drooling is a jewel.";
            imageLinks =                 {
                smallThumbnail = "http://bks8.books.google.com/books?id=LdB2_WzYpKgC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api";
                thumbnail = "http://bks8.books.google.com/books?id=LdB2_WzYpKgC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api";
            };
            industryIdentifiers =                 (
                                    {
                    identifier = 0836218256;
                    type = "ISBN_10";
                },
                                    {
                    identifier = 9780836218251;
                    type = "ISBN_13";
                }
            );
            infoLink = "http://books.google.com/books?id=LdB2_WzYpKgC&dq=isbn:0836218256&hl=&source=gbs_api";
            language = en;
            pageCount = 127;
            previewLink = "http://books.google.com/books?id=LdB2_WzYpKgC&printsec=frontcover&dq=isbn:0836218256&hl=&cd=1&source=gbs_api";
            printType = BOOK;
            publishedDate = "1988-01-01";
            publisher = "Andrews McMeel Publishing";
            ratingsCount = 18;
            readingModes =                 {
                image = 1;
                text = 0;
            };
            title = "Something Under the Bed Is Drooling";
        };
    }
    );
    kind = "books#volumes";
    totalItems = 1;
}

我想要做的是从这本词典中的项目创建一个对象。这种方法需要一般地为所有书籍工作而不会崩溃。即使他们缺少一些属性。我需要提取的属性是:

-authors姓氏(如果有多个作者,则需要列出每位作者)

-authors first name(如果有多个,则需要列出所有作者)

-title

-description

-smallThumbnail

-thumbnail

-isbn_10标识符

-category

从这些我将要创建一本"书"宾语。我还将创建一个NSSet of Authors,每个作者都有一个lastName和firstName

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您应该能够安全地使用NSDictionary的-valueForKeyPath:从项目dict中检索每个值:

NSArray *names = [itemDict valueForKeyPath:@"volumeInfo.authors"];
NSString *title = [itemDict valueForKeyPath:@"volumeInfo.title"];
...