[英]can not access the url inside of array , JSON response , using Alamofire & SwiftyJSON
[英]link gives back nil and crashes my app when using Alamofire and SwiftyJSON to get JSON from URL
我是在線提出請求的新手。 我正在使用Alamofire和swiftyJSON從URL取回數據,但是它一直崩潰,並說鏈接返回了零。 是因為提供的鏈接末尾不是.json? (順便說一句,我添加的鏈接是假的,無法添加真實的鏈接,但結構相同)。
let URL = "https://www.thisurlisntreal.com/api/folder?key="12345FGhbfdgn3456fgdnf2345redfs?56"
Alamofire.request(.GET, URL).validate().responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
print("JSON: \(json)")
}
case .Failure(let error):
print(error)
}
}
該鏈接僅包含以下數據:
{"results":[{"id":1,"fromDate":"2015-03-18","toDate":"2016-04-23","url":"blabliebloe","producer":{"id":1,"name":"theproducer"}}]}
我在Alamofire.request(.GET, URL).validate().responseJSON { response in
時Alamofire.request(.GET, URL).validate().responseJSON { response in
崩潰,告訴我unexpectedly found nil while unwrapping an Optional value
。 我用多個格式化程序和驗證程序檢查了JSON,並且都成功了,所以這不是問題。
更新:我只是使用了一個不同的鏈接( http://refine.taxonomics.org/gbifchecklists/reconcile?query=Crepidomanes%20minutum ),它確實可以正常工作...我不知道這是怎么可能的,但是可以嗎?服務器端相關?
嘗試像這樣替換開關盒:
switch response.result {
case .Success(let data):
let json = JSON(data)
case .Failure(let error):
print(error.localizedDescription())
}
也許您應該檢查您的URLRequest是否設置正確,以期望使用以下任一行的json答案:
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.