【问题标题】:How to send array of dictionarys in swift by using alamafire(Post method)如何使用 alamofire(Post 方法)快速发送字典数组
【发布时间】:2016-08-11 07:02:04
【问题描述】:

我遇到了一个问题,我的服务器期望来自应用程序端的字典数组。请建议我在这里出错的地方,下面的代码

    {
 let param : [String : AnyObject]  =
            ["trf_id"  :  Constant.constantVariables.trfID,
             "mode"              : Constant.modeValues.createMode,
             "to_city"           : Constant.constantVariables.to_city,
             "from_city"         : Constant.constantVariables.from_city,
             "description"       : Constant.constantVariables.descrption,
             "request_type"      : Constant.constantVariables.request_type,
             "to_date"           : Constant.constantVariables.to_date!,
             "from_date"         : Constant.constantVariables.from_date!,
             "travel_configs"    :  ["Config_id" : "9","values" : "train",
                                      "Config_id" : "10","values" : "bus"]]
        print(param)
}
  • 其中 travel_configs 是字典数组

由于服务器异常,我必须这样发送它

trf_id:37
mode:1
request_type:0
from_city:sdfsd
to_city:qws
from_date:2016-08-17
to_date:2016-08-26
description:sdfsdf
travel_configs:[
{"config_id":"11","values":"1"}
,{"config_id":"2","values":"Flight"}]

【问题讨论】:

  • travel_configs 是字典。 [["Config_id" : "9","values" : "train"], ["Config_id" : "10","values" : "bus"]] 将使其成为字典数组。
  • @ShadowOf 是的,我正在制作字典数组,但我的服务器不接受我必须像期望服务器一样发送的字典。
  • @ShadowOf 并且发送的参数都变空了。

标签: ios swift dictionary alamofire post-parameter


【解决方案1】:

经过3天的挣扎,我终于得到了答案

只需将您的字典数组发送到此 类 func JSONStringify(value: AnyObject,prettyPrinted:Bool = false) -> String{

    let options = prettyPrinted ? NSJSONWritingOptions.PrettyPrinted : NSJSONWritingOptions(rawValue: 0)


    if NSJSONSerialization.isValidJSONObject(value) {

        do{
            let data = try NSJSONSerialization.dataWithJSONObject(value, options: options)
            if let string = NSString(data: data, encoding: NSUTF8StringEncoding) {
                return string as String
            }
        }catch {

            print("error")
            //Access error here
        }

    }
    return ""

}

希望这对其他人有帮助谢谢..

【讨论】:

    【解决方案2】:

    首先,它可能来自大写的“C”而不是“config_id”中的“c”?

    其次,您实际上缺少 [],您的代码应如下所示:

    let param : [String : AnyObject]  =
                ["trf_id"  :  Constant.constantVariables.trfID,
                 "mode"              : Constant.modeValues.createMode,
                 "to_city"           : Constant.constantVariables.to_city,
                 "from_city"         : Constant.constantVariables.from_city,
                 "description"       : Constant.constantVariables.descrption,
                 "request_type"      : Constant.constantVariables.request_type,
                 "to_date"           : Constant.constantVariables.to_date!,
                 "from_date"         : Constant.constantVariables.from_date!,
                 "travel_configs"    :  [["Config_id" : "9","values" : "train",
                                          "Config_id" : "10","values" : "bus"]]]
    

    为什么? 因为您的服务器需要一个字典数组,而您只发送一个字典;)

    【讨论】:

    • 有时我不明白downvotes -_-' 致投反对票的人:网站邀请您解释投反对票,以便人们理解他们的错误;)
    猜你喜欢
    • 2018-07-22
    • 2015-03-03
    • 2018-01-08
    • 2018-07-18
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    相关资源
    最近更新 更多