一直在研究解析相當多。隨着信息過多即時拍攝的JSON似乎沒有任何解釋如何在一個合理的方式做以提取與SWIFT 3.如何解析api的swift 3?
信息這是走到這一步,
func getBookDetails() {
let scriptUrl = "https://www.googleapis.com/books/v1/volumes?q=isbn:9781451648546" .
let myurl = URL(string:scriptUrl)
let request = NSMutableURLRequest(url: myurl!)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: myurl!) { (data, response, error) in
if error != nil{
print("THIS ERROR",error!)
return
} else{
if let mydata = data{
do{
let myJson = try (JSONSerialization.jsonObject(with: mydata, options: JSONSerialization.ReadingOptions.mutableContainers)) as AnyObject
// print("this is the MY JSON",myJson) ---> prints out the json
if let dictonary = myJson["items"] as AnyObject? {
print("the DICTONARY",dictonary) // ----> OUTPUT
if let dictonaryAA = dictonary["accessInfo"] as AnyObject? {
print("the accessInfo",dictonaryAA)
}
}
} catch{
print("this is the in CATCH")
}
} //data
}
}
task.resume()
}
}
OUTPUT :
the DICTONARY (
{
accessInfo = {
accessViewStatus = SAMPLE;
country = US;
=============
RELEVANT DATA as in https://www.googleapis.com/books/v1/volumes?
q=isbn:9781451648546"
==========================
title = "Steve Jobs";
};
}
)
只需要通過JSON數據解析參考isbn獲得本書的名稱,作者和書名。 知道應該有更好的方式做的事情,是很容易理解的人新進入語言
嘿,你會檢查此鏈接https://www.binpress.com/tutorial/swiftyjson-how-to-handle-json-in-swift/111 –
@Dev_Tandel,它不完全是答案,但該鏈接提供了一些註釋,清除了一些關於JSON的查詢,謝謝 –
我知道這不是所評論的答案。我認爲它可能會幫助你 –