Swift不能从jsonResult返回准确的数据



我试图从谷歌图书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
}

相关内容

  • 没有找到相关文章

最新更新