How to send array of dictionarys in swift by using alamafire(Post method)

765 Views Asked by At

I am running with an issue where my server is expecting an array of dictionarys from the app side. Please suggest where I'm going wrong here, code below

    {
 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)
}
  • Where travel_configs is array of dictionaries

I have to send it like this because of server exceptions

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

There are 2 best solutions below

0
On BEST ANSWER

Finally i got answer after 3 days of struggling

just send your array of dictionary into this class 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 ""

}

Hope this will help some one else Thank you..

1
On

First, it might come from the capital "C" instead of "c" in "config_id" ?

Secondary, you are actually missing [], your code should look like this :

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

Why? Because your server is expecting an array of dictionary, and you are sending only a dictionary ;)