我尝试创建 JSON 格式的字典数组并将其发送到 Alamofire。
{
"attendance": [{
"attndid":"0",
"psngrtype": "student",
"date":currentdate,
"cuid": "25",
"enttid": "21",
}] }
在这里,我使用了表格 View ,在上面的“cuid”和“enttid”中,我从可选择的表格 View 单元格数据中给出了值。其余为常数。如果我选择一个 TableView 单元格数据,我将传递一组字典。和两个数组,如果我选择两个单元格等等。 我正在使用下面的代码,但我得到了创建字典格式的问题。 我的代码:
let arrayOfDictionaries: [[String:AnyObject]] =
[
["psngrtype":"student" as AnyObject,
"attndid": "0" as AnyObject ,
"cuid":stdpasngerid as AnyObject,
"enttid": entitID as AnyObject,
"attnddate":CurrentDate as AnyObject ]]
print(arrayOfDictionaries.toJSONString())
extension Collection where Iterator.Element == [String:AnyObject] {
func toJSONString(options: JSONSerialization.WritingOptions = .prettyPrinted) -> String {
if let arr = self as? [[String:AnyObject]],
let dat = try? JSONSerialization.data(withJSONObject: arr, options: options),
let str = String(data: dat, encoding: String.Encoding.utf8) {
return str
}
return "[]"
} }
输出:
[{
"enttid" : "1",
"psngrtype" : "student",
"attnddate" : "10-26-2017",
"attndid" : "0",
"cuid" : "25" }]
我想添加第一个,如 json 格式。如果我选择多个 TableView 单元格,则添加多个数组。请帮助我被困住了?
最佳答案
你就能做到
var finalArray:[String:String] = [:]
finalArray["Attandance"] = arrayOfDictionaries.toJSONString()
关于ios - 如何在 swift 3 中使用 Alamofire 制作并发送字典数组(JSON 格式)发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46949490/