【发布时间】:2017-10-26 08:38:15
【问题描述】:
我尝试创建字典 JSON 格式数组并将其发送到 Alamofire。
{
"attendance": [{
"attndid":"0",
"psngrtype": "student",
"date":currentdate,
"cuid": "25",
"enttid": "21",
}] }
在这里,我使用了tableview,在上面的“cuid”和“enttid”中,我从可选的tableview单元格数据中给出了价值。剩余的都是常数。如果我选择一个表格视图单元格数据,我将传递一组字典。和两个数组,如果我选择两个cells.etc .. 我正在使用下面的代码,但我得到了创建字典格式的问题。 我的代码:
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 单元格,则添加多个数组。请帮助我卡住了?
【问题讨论】:
-
不相关,但服务器根本不关心漂亮打印的 JSON。
-
@vadian 谢谢!所以,你能告诉我一些例子吗?如果不是我的。
-
我只是想把默认的写作选项改成
[] -
@vadian 是的,我做到了。但服务器不接受我的格式。它接受上述格式。那是我的问题。
标签: ios arrays json swift alamofire