【问题标题】:Get info JSON Google Books - IOS获取信息 JSON Google 图书 - IOS
【发布时间】:2016-08-11 17:07:29
【问题描述】:

如何使用 Swift 中的 JSON 从谷歌的书中获取作者?此代码有效,但我对作者有意见。

这是 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 中都有作者
  • 是的,这些作者存在。我认为错误可以链接到数组[“Joseph Hansen”]
  • 我将发布一些太长的代码,无法发表评论,但它演示了 API 的工作方式并以与您在此处相同的方式返回作者。它已经过测试并且可以工作@Carlo

标签: ios arrays json swift google-books


【解决方案1】:

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

首先,我只是为我发送给它的 URL 创建 API 返回的所有书籍的数组...

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

但是获取作者的代码会给你一个包含所有作者姓名的数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2015-09-25
    相关资源
    最近更新 更多