我正在尝试从Google图书API中提取图书数据:
https://www.googleapis.com/books/v1/volumes?q=isbn:9781451648546

我能够获得书名,描述和缩略图,但是我一直想获得作者和类别的确切信息(没有[“”])。

我通过上一个链接得到了这个结果:

Author:["Walter Isaacson"]
Categories:[""Biography & Autobiography""]

    "volumeInfo": {
    "title": "Steve Jobs",
    "authors": [
     "Walter Isaacson"
    ],
    "publisher": "Simon and Schuster",
    "publishedDate": "2011",


并在我的iOS应用程序上使用以下代码:

if let arrayOfAuthors = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.authors") as? [[String]] {
            DispatchQueue.global(qos: .userInitiated).async {
                // Bounce back to the main thread to update the UI
                DispatchQueue.main.async {
                    self.authorLabel.text =  "Author: \(arrayOfAuthors[0])"
                }
            }

        }

最佳答案

这对我有用

func parseJSON(){
    let backgroundQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default)

    backgroundQueue.async(execute: {

        let serverResponseJSON:NSDictionary = self.parseURLToResponseJSON(urlToRequest: "https://www.googleapis.com/books/v1/volumes?q=isbn:9781451648546")
        print(serverResponseJSON)
        let arrayOfauthors:NSArray = serverResponseJSON.value(forKeyPath: "items.volumeInfo.authors") as! NSArray
        print(arrayOfauthors)

        DispatchQueue.main.async {
            self.authorLabel.text =  "Author: \(arrayOfAuthors[0])"
        }
    })
}

func parseURLToResponseJSON(urlToRequest: String) -> NSDictionary{
    let inputData = try? Data(contentsOf: URL(string: urlToRequest)!)
    let boardsDictionary: NSDictionary = (try! JSONSerialization.jsonObject(with: inputData!, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSDictionary
    return boardsDictionary
}

09-25 19:20