2014-09-17 2 views
0

J'ai ce NSDictionary que j'ai reçu un appel api:Articles Accès Nested 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; 
} 

Ce que je veux faire est de créer un objet à partir des éléments dans ce dictionnaire. Cette méthode doit fonctionner de manière générique pour tous les livres sans se bloquer. Même s'il leur manque certains attributs. Les attributs dont j'ai besoin d'extraire sont:

-Auteurs nom de famille (besoin pour chaque auteur énuméré s'il y a plus d'un)

-Auteurs prénom (Nécessité de chaque auteur énuméré s'il y a plus d'un)

-title

-description

-smallThumbnail

-thumbnail

-isbn_10 Identifiant

-catégorie

De ce que je vais créer un objet "livre". Je vais également créer un NSSet d'auteurs, et chaque auteur a un nom et un prénom

Comment puis-je faire cela?

Répondre

1

Vous devriez pouvoir utiliser en toute sécurité -valueForKeyPath: de NSDictionary pour récupérer chaque valeur d'un élément dict:

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

Il ne fonctionne pas; ce titre est nul. – Rose

+0

Vraiment? Je viens de le tester et ça marche pour moi. Pour être clair, voici ce que je fais: '[@ {" accessInfo ": @ {" title ": @" titre de l'échantillon "}} valueForKeyPath: @" accessInfo.title "];'. – ryanipete