Ios 如何使用alamafire(Post方法)在swift中发送字典数组

Ios 如何使用alamafire(Post方法)在swift中发送字典数组,ios,swift,dictionary,alamofire,post-parameter,Ios,Swift,Dictionary,Alamofire,Post Parameter,我遇到了一个问题,我的服务器希望从应用程序端获得一系列字典。请指出我的错误所在,代码如下 { let param : [String : AnyObject] = ["trf_id" : Constant.constantVariables.trfID, "mode" : Constant.modeValues.createMode, "to_city" :

我遇到了一个问题,我的服务器希望从应用程序端获得一系列字典。请指出我的错误所在,代码如下

    {
 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_配置的是字典数组
由于服务器异常,我必须这样发送它

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

首先,它可能来自大写字母“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"]]]
为什么??
因为您的服务器需要一个字典数组,而您只发送一个字典;)

经过3天的努力,我终于得到了答案

只需将您的字典数组发送到此 类func JSONStringify(值:AnyObject,预打印: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 ""

}

希望这对其他人有所帮助谢谢。

travel\u configs is dictionary。[[“配置id”:“9”,“值”:“train”],[“配置id”:“10”,“值”:“bus”]]将使其成为字典数组。@ShadowOf是的,我正在生成字典数组,但我的服务器不接受我必须从服务器发送的字典。@ShadowOf和曾经发送的参数将变为null。有时我不理解对否决的人的否决票--’:网站邀请您访问e解释否决票,以便人们能够理解他们的错误;)