Alamofire POST 请求:responseSerializationFailed

时间:2021-01-07 18:25:37

标签: swift alamofire alamofire-request

我在使用 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.})))

这是怎么回事?非常感谢您的阅读

1 个答案:

答案 0 :(得分:0)

您可以使用 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
        ]