【问题标题】:Alamofire POST request: responseSerializationFailedAlamofire POST 请求:responseSerializationFailed
【发布时间】:2021-01-07 18:25:37
【问题描述】:

我在使用 swift 时遇到问题,因为我需要像这样发送Json

 {
   "package": {
     "description": "Pink iPad",
     "contentValue": 120.01,
     "weight": 1.01,
     "length": 30.01,
     "height": 15.01,
     "width": 20.01
   },
   "origin_zip_code": "44100",
   "destination_zip_code": "44510"
 }
 

这是我的尝试:

   let package : [String: Any?] = [
    "description": "Pink iPad",
        "contentValue": 120.0,
        "weight": 1.01,
        "length": 30.01,
        "height": 15.01,
        "width": 20.01
    ]

    
     parameters = [
        "package": package,
        "origin_zip_code": "44100",
        "destination_zip_code": "44510"
    ]

当我收到 Alamofire 的回复时,我收到以下消息:

failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))

它有什么问题?非常感谢您的阅读

【问题讨论】:

  • 您可以在发送请求之前打印您的参数,并且快速浏览一下它似乎是正确的。看一下 api 并在 postman 中测试请求,响应和格式是什么。
  • 您是否尝试编码以发送或解码响应?您的代码似乎建议您重新编码,错误提示您正在解码

标签: swift alamofire alamofire-request


【解决方案1】:

你可以使用Parameters Object:

 let package: Parameters = [
            "description" : "Pink iPad",
                "contentValue": 120.0,
                "weight": 1.01,
                "length": 30.01,
                "height": 15.01,
                "width": 20.01
        ]

let parameters : Parameters = [
            "origin_zip_code": "44100",
            "destination_zip_code": "44510",
            "package": package
        ]

【讨论】:

    猜你喜欢
    • 2019-02-22
    • 2018-02-27
    • 1970-01-01
    • 2018-06-04
    • 2018-07-27
    • 2018-04-10
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多