Alamofire快速接收和解析一串字符串

时间:2017-02-28 05:33:50

标签: ios iphone swift swift3

我将结果作为像这样的字符串数组

["India","America","Australia","China","Russia"]

我正在使用 Alamofire 来使用代码获取响应。没有错误,但我得到的结果为null。请帮助解析这个。

sessionManager?.request(strURL, method: method, parameters: params, encoding: encoding , headers: headers).responseJSON { (response) in
   switch response.result {
        case .success:
            let resJson = JSON(response.result.value!)
           success(resJson)
            break
        case .failure(let error):
            failure(error as NSError)
            break
        }

    }

3 个答案:

答案 0 :(得分:2)

试试这个:

if let responseData = response.result.value{    
let responsevalue = responseData  as? [String]
}

答案 1 :(得分:0)

对于寻找另一个派生答案的人,只需将这段代码放在Alamofire.request(...)之后:

.responseJSON(completionHandler: { (response) in

            switch response.result{

            case .success(let value):

                // Here is your array of String
                let arrayOfStrings = value as? [String]

            case .failure(let error):

                // Some code when error happens...
                print(error.localizedDescription)

            }

        })

答案 2 :(得分:0)

使用SwiftyJSON的此解决方案:

.responseJSON(completionHandler: { (response) in

     switch response.result{
     case .failure(let error):
         print(error.localizedDescription)

     case .success(let res):
       let json = JSON(res)
       let res = json["result"]

       var models = [String]()
       if let models1 = company["models"].array {
           for model in models1 {
               guard let mod = model.string else { return }
               models.append(mod)
           }
       }
     }
})