SwiftのJSONを使用してGoogleの書籍の著者を取得するにはどうすればよいですか?このコードは機能しますが、著者には問題があります。情報を取得するJSON Googleブックス - IOS
これはJSONの例である:getBookInfo FUNC
{
"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(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の作者がありません – pbush25
はい、これらの著者が存在します。私は、エラーが配列["Joseph Hansen"]にリンクできると思います。 – Carlo
コメントのためには長すぎるコードを投稿しますが、これはAPIの動作を示し、Authorsを同じ方法で返しますここに。テスト済みで、このように@Carlo – pbush25