参数Encoder失败,因为invalidRootObject?

时间:2019-05-10 20:32:39

标签: json swift post encoding alamofire

当我尝试向Web API发送JSON字符串'parametersString'时,parameterEncoderFailed出现错误消息,有人可以帮助我解决问题

  

失败(Alamofire.AFError.parameterEncoderFailed(原因:   Alamofire.AFError.ParameterEncoderFailureReason.encoderFailed(错误:   Alamofire.URLEncodedFormEncoder.Error.invalidRootObject(“ string(\” {\ n \\“ authenticate \\”:   {\ n \\“ userName \\”:\\“ fan.doe \\”,\ n \\“ password \\”:   \\“ 12345 \\”,\ n \“到期\\”:\\“ 2019-05-03   18:00:00 \\“ \ n} \ n} \”)“)))))))

 static func Login(email:String, password:String, completion: @escaping (_ success: Bool, _ response: DataResponse<Data?>?) -> ()) {
        let urlString: 

        let parametersString = """
        {
        "authenticate": {
        "userName": "\(email)",
        "password": "\(password)",
        "expiry": "2019-05-03 18:00:00"
        }
        }
        """
         var response:DataResponse<Data?>?

        print(urlString)
        AF.request(urlString, method: .post, parameters: parametersString, encoding:  String.Encoding.utf8.rawValue,  headers: headers )
            .response { resp in
                response = resp
                print(response)
                if(response?.response?.statusCode == 200){
                    completion(true, response)

                }
                else {
                    completion(false, nil)
                }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您可能需要发送字典[String:Any]

let parametersDic:[String:Any] = [
    "authenticate": [
    "userName": "\(email)",
    "password": "\(password)",
    "expiry": "2019-05-03 18:00:00"
]]