获取信息 JSON Google 书籍 - IOS

Get info JSON Google Books - IOS

如何从 google 的 JSON 在 Swift 的书中找到作者?此代码有效,但我对作者有疑问。

这是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()
        }
    }

提前致谢。

所以为了检索作者,我做了一些类似但又有点不同的事情。

首先,我只是创建了一个数组,其中包含 API 为我发送给它的 URL 返回的所有书籍...

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
    }
} 

然后在我的数据转换功能中,我循环遍历所有结果,并获得我想要的值。

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
    }
}

但是获取作者的代码将为您提供所有作者姓名的数组。