Search code examples
iosarraysjsonswiftgoogle-books

Get info JSON Google Books - IOS


how can I get authors from a google's book with JSON in Swift? This code works but I have a problem with authors.

This is an example of JSON:

 {
 "kind": "books#volume",
 "id": "17BYrgEACAAJ",
 "etag": "snlcLszwWRo",
 "selfLink": "https://www.googleapis.com/books/v1/volumes/17BYrgEACAAJ",
 "volumeInfo": {
  "title": "Scomparso. Dave Brandstetter mysteries",
  "authors": [
   "Joseph Hansen"
  ],
  "publisher": "Elliot",
  "publishedDate": "2012-01",

func getBookInfo:

 func getBookInfo(isbn: String) {

        let url_book = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn

        if let url = NSURL(string: url_book) {
            NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: {data, _, error -> Void in
                if let error = error {
                    print(error.localizedDescription)
                } else {
                    let data = data,
                    jsonResult = try? NSJSONSerialization.JSONObjectWithData(data!, options: []),
                    arrayOfTitles = jsonResult!.valueForKeyPath("items.volumeInfo.title") as? [String],
                    arrayOfAuthors = jsonResult!.valueForKeyPath("items.volumeInfo.authors") as? [String],
                    arrayOfPublishers = jsonResult!.valueForKeyPath("items.volumeInfo.publisher") as? [String],
                    arrayUrlImageBook = jsonResult!.valueForKeyPath("items.volumeInfo.imageLinks.smallThumbnail") as? [String]
                    self.titles = arrayOfTitles!.joinWithSeparator(", ")
                    self.authors = arrayOfAuthors!.joinWithSeparator(", ")
                    self.publishers = arrayOfPublishers!.joinWithSeparator(", ")
                    let url_image_book: String! = arrayUrlImageBook!.joinWithSeparator(", ")
                    self.title_label.text = self.titles
                    self.author_label.text = self.authors
                    if self.publishers != nil{
                        self.publisher_label.text = self.publishers
                    }
                    self.book_image.downloadedFrom(url_image_book!)

                }
            }).resume()
        }
    }

Thanks in advance.


Solution

  • So in order to retrieve the authors, I do something similar but a little bit different.

    First, I just create an array of all the books that are returned by the API for the URL that I send to it...

    if let volumeInfo = jsonResult.valueForKeyPath("items.volumeInfo") as? [AnyObject] {
        if volumeInfo.count != 0 {
            self.volumeInfo = volumeInfo
            self.translateJSONData() //here is where I then translate the data
        }
    } 
    

    Then in my data translation function, I loop through all the results, and get the values I want.

    for apiResponse in self.volumeInfo {
        if let singleVolumeInfo = apiResponse as? [String : AnyObject] {
            let bookCategory = singleVolumeInfo["categories"] as? [String]
            let bookAuthors = singleVolumeInfo["authors"] as? [String]
            //more information here
        }
    }
    

    But that code to get the authors will give you an array of all the author's names.