如何将Object参数数组传递给Alamofire swift

问题描述 投票:0回答:2

我是Swift的新手,任何人都可以帮助我。

我想将对象数组传递给Alamofire,但我不知道该怎么做

这里是所需参数:

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZWU4OGZiNzhiYTBkMjMyZDFmYWZkMzgiLCJpYXQiOjE1OTIyOTk2Njh9.AVuxiTZy10fV2ZMZcT-oHXSg6PdK3tfE",
    "zipCodes": [
        {
            "zip_code": "55001",
            "city": "Afton",
            "state": "MN",
            "county": "Washington"
        }
    ]
}

我这样做

 let parameters : [String : String] = ["token" : retrivedToken, "zipCodes" : [{
            "zip_code": "55001",
            "city": "Afton",
            "state": "MN",
            "county": "Washington"
            }]
        ]

ios swift alamofire
2个回答
0
投票

只需在请求中使用您的参数即可。

func sendRequestRequest() {
// JSON Body
let parameters: [String : Any] = [
    "token": retrivedToken,
    "zipCodes": [
        "county": "Washington",
        "state": "MN",
        "zip_code": "55001",
        "city": "Afton"
    ]
]

// Fetch Request
Alamofire.request("your API url", method: .post, parameters: parameters, encoding: JSONEncoding.default)
    .validate(statusCode: 200..<300)
    .responseJSON { response in
        if (response.result.error == nil) {
            print("HTTP Response Body: \(response.data)")
        }
        else {
            print("HTTP Request failed: \(response.result.error)")
        }
    }

}

不要忘记,如果您使用Alamofire v5,请使用AF.request而不是Alamofire.request


0
投票

//在这里定义参数让bodyParams:[字符串:任何] = [ “ token”:“(retrivedToken)”, “ zipCodes”:[ “ county”:“华盛顿”, “ state”:“ MN”, “ zip_code”:“ 55001”, “ city”:“ Afton” ] ] }

// Alamofire请求在这里

let urlString =“ abc.com”

[Alamofire.request(urlString,方法:.post,参数:bodyParams,编码:JSONEncoding.default,标头:nil).responseJSON {回应 切换response.result { 大小写成功: 打印(响应)

                break
            case .failure(let error):

                print(error)
            }

}

© www.soinside.com 2019 - 2024. All rights reserved.