所以我试图解析Google Books API的回应。我想获得标题,描述,thumbnailUrl,作者和发布的数据。这是问题所在:
func getBooksFrom(completion: @escaping (Result<[[String: AnyObject]]>) -> Void) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil else { return completion(.Error(error!.localizedDescription)) }
guard let data = data else { return
completion(.Error(error!.localizedDescription)) }
do {
if let json = try JSONSerialization.jsonObject(with: data, options: [.mutableContainers]) as? [String: AnyObject] {
if let items = json["items"] as? [[String: AnyObject]] {
DispatchQueue.main.async {
completion(.Succes(items))
}
}
}
} catch let error {
print(error.localizedDescription)
return completion(.Error(error.localizedDescription))
}
}.resume()
}
在ViewDidLoad的View Controller中我有
let service = ApiService()
service.getBooksFrom { (result) in
switch result {
case .Succes(let data):
self.parseData(array: data)
case .Error(let message):
self.showAlertWith(title: "Error", and: message)
}
}
这样很简单的解析,但...... 当我想将项目映射到Book Object时,我必须:
func parseData(_ data: [[String: AnyObject]]) -> [Book]{
for item in data {
if let volumeInfo = item["volumeInfo"] as? [String: AnyObject] {
let books = data.map { (jsonDictionary) -> Book in
let title = volumeInfo["title"] as? String ?? ""
let publishedData = volumeInfo["publishedDate"] as? String ?? ""
let authors = volumeInfo["authors"] as? [String] ?? [""]
let description = volumeInfo["description"] as? String ?? ""
let newBook = Book(title: title, publishedData: publishedData, description: description)
return newBook
}
return books
}
}
return [Book]()
}
这是非常糟糕的方式。你必须在底部返回Book,因为for循环,和 VolumeInfo是下一个词典,所以我真的不知道如何映射它并获得例如作者,因为它是下一个数组..
一个示例JSON对象:
{
"items":[
{
"volumeInfo":{
"title":"The Ancestor's Tale",
"subtitle":"A Pilgrimage to the Dawn of Life",
"authors":[
"Richard Dawkins",
"Yan Wong"
]
"publishedDate":"2016-04-28",
"description":"A fully updated ",
"imageLinks":{
"smallThumbnail":"http://books.google.com/books/content?id=vzbVCQAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail":"http://books.google.com/books/content?id=vzbVCQAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
}
}
]}
所以当你有String:Value数组时,这很简单,但是如果你有字典VolumeInfo中的字典或像作者这样的字符串数组,你应该如何正确映射?
答案 0 :(得分:1)
我个人发现使用URLSession在swift中解析对象的方法比较笨拙。每当我可以将Alamofire与AlamofireObjectMapper结合使用。
这允许您创建一个简单的对象。例如:
class Book: Mappable {
var title: String?
var subtitle: String?
var description: String?
required init?(map: Map){
}
func mapping(map: Map) {
title <- map["title"]
subtitle <- map["subtitle"]
description <- map["description"]
}
}
当您发出请求时,您可以使用responseObject方法直接解析您的对象并分配正确的类型。
Alamofire.request(URL).responseObject { (response: DataResponse<Book>) in
let book = response.result.value
print(book?.title)
}
对于这个例子,我只解析了一本书。但是这个概念也可以很容易地扩展到数组或嵌套的json对象。我个人觉得这比直接使用URLSession的代码更清晰。