Alamofire请求响应?
我用这Alamofire请求并返回一个JSONAlamofire请求响应?
let todoEndpoint: String = "https://api.abc.com/api/v3/products?pid=uid8225&format=json&&offset=0&limit=10"
Alamofire.request(todoEndpoint)
.responseJSON { response in
guard let json = response.result.value as? [String: Any] else {
print("didn't get todo object as JSON from API")
print("Error: \(response.result.error)")
return
}
print(json)
}
现在我有做的循环,我想用这个JSON的价值,但我得到:
错误:使用未解决的标识符的JSON ?
do {
for (index,subJson):(String, JSON) in json {
print("Index :\(index) Title: \(subJson)")
} catch
{
print("there was an error")
}
如前所述:
https://github.com/Alamofire/Alamofire#response-string-handler “的请求的结果是唯一的响应盖的范围内可在响应或数据的任何执行队伍从服务器接收到必须做在答复结束之内“。
如何在响应关闭范围外使用此json值?
能否请你建议
有没有完成处理程序,我需要写和怎么能做到呢?
感谢
更好地利用SwiftyJson方便JSON与Almofire解析像波纹管:
import Alamofire
import SwiftyJSON
static func getRequest(urlString: URL?, Parameter:NSDictionary?, completion: @escaping (_ serverResponse: AnyObject?,_ error:NSError?)->()){
Alamofire.request(urlString!, parameters:nil, headers: nil).responseJSON { response in
if(response.result.value != nil){
let serverResponse = JSON(response.result.value!)
print("Array value is \(serverResponse.arrayValue)")
completion(serverResponse as AnyObject?, nil)
}
else{
completion(nil, response.result.error as NSError?)
}
}
}
你可以像NetworkLayer一个单独的类写这个函数,然后从任何类调用它为WebService呼叫如下:
let url = NSURL(string: yourWebServiceUrlString)
NetworkLayer.getRequest(urlString: url as URL?, Parameter: nil) { (serverResponse, error) in
if (error == nil){
print("Server response: \(serverResponse)")
}
}
注意:您也可以传递参数字典。
避免应答以前多次询问过的重复。 – 2017-04-12 12:15:37
好的,我会关心这一点。 – Aashish1aug
嗨偷偷摸摸,这个问题有点不同于你已经提到的线程,因为我通过链接搜索,但无法得到答案 –
[使用新的Swift 3和Alamofire解析JSON](http://*.com/questions/39468516/parsing-json-using-the-new-swift-3-and-alamofire) – 2017-04-12 12:13:50
GS ,您是否尝试搜索解决方案,因为此问题已被询问为超过30多个主题:https://www.google.se/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q= alamofire + json + response + swift + * – 2017-04-12 12:14:35
你应该在后卫后面跑,即'guard let else {...} HERE'。目前你在'json'不存在的范围内执行它 – Honey