简体   繁体   English

Alamofire POST 请求:responseSerializationFailed

[英]Alamofire POST request: responseSerializationFailed

I have a problem using swift because I need to send a Json like this:我在使用 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"
 }
 

That's my attempt to do that:这是我的尝试:

   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"
    ]

When I got a response from Alamofire I have the following message:当我收到 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.})))

What's wrong with it?它出什么问题了? Thank you so much for reading非常感谢您的阅读

You could use Parameters Object :您可以使用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
        ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM