【问题标题】:how to send a dictionary as a parameter in Alamofire, swift 3如何在 Alamofire,swift 3 中将字典作为参数发送
【发布时间】:2016-10-31 06:11:20
【问题描述】:

我想发送下面的参数

{"serial_number":{"type":"match", "value":"somevalue"}}

所以我就这样尝试

 let parameters : [String:AnyObject] = [
            "serial_number":[
                "type":"match",
                "value":serialNum
            ]
        ]

然后像这样'GET'

Alamofire.request(.GET, Constant.WebClient.Cool + "/creator/" + cid + "/wa?filling=", parameters: nil, headers: headers)
            .responseJSON { response in

                switch response.result {
                case.Success(let JSON):
                    print("get json is success")
                    if JSON["status"] != nil && JSON["status"] as! String == "fail"{
                        print("some error occured")
                    }
                    else{
                        let resultArray = JSON as! NSArray
                        print("\(resultArray)")
                    }
                    break
                case .Failure(let error):
                    print("request failed with error : \(error.localizedDescription)")

                }

        }

但对我不起作用。如何获得

{"serial_number":{"type":"match", "value":"someval"}}

到字典。希望您对此有所帮助。当我尝试打印字典时,如下所示

["serial_number": {
    type = match;
    value = "some val";
}]

希望您对此有所帮助。

【问题讨论】:

    标签: parameters get swift3 nsdictionary alamofire


    【解决方案1】:

    alamofire 请求使用默认编码,即URLEncoding.default,在这种情况下无法按预期工作,因此您必须将编码更改为JSONEncoding(options: [])

    关注文档:https://github.com/Alamofire/Alamofire#json-encoding

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多