如何使用 alamofire(Post 方法) 在 swift 中发送字典数组
How to send array of dictionarys in swift by using alamafire(Post method)
我是 运行 我的服务器需要来自应用程序端的一系列字典的问题。请在这里指出我哪里出错了,代码如下
{
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"}]
首先,它可能来自首都 "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天终于得到答案
只需将您的字典数组发送到这里
class func JSONStringify(value: AnyObject,prettyPrinted:Bool = false) -> 字符串{
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 ""
}
希望这对其他人有所帮助谢谢..
我是 运行 我的服务器需要来自应用程序端的一系列字典的问题。请在这里指出我哪里出错了,代码如下
{
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"}]
首先,它可能来自首都 "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天终于得到答案
只需将您的字典数组发送到这里 class func JSONStringify(value: AnyObject,prettyPrinted:Bool = false) -> 字符串{
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 ""
}
希望这对其他人有所帮助谢谢..