我想创建一个json字符串并将其发送到服务器。我想发送我在Alamofire库中提到的JSON。现在的问题是如何在swift编程中创建这个json字符串?
{"Notification":
[
{"id":"15","TableName":"HadafShakhsi","RowId":"148424-1","Operation":"2","StoreName":"SelectHadafShakhsi","isFirst":"1"}
]
}
答案 0 :(得分:3)
let parameters: [String: AnyObject] = [
"Notification": [
"id": "15",
..........
......
]
]
Alamofire.request(.POST, "http://server.com", parameters: parameters, encoding: .JSON)
.responseJSON { request, response, JSON, error in
print(response)
print(JSON)
print(error)
}
答案 1 :(得分:0)
您可以将函数prepareforObject设为
func prepareObjects(dict : Dictionary<String, Any> ) -> [String: AnyObject] {
var dictParameters = [String: AnyObject]()
for (key, value) in dict {
dictParameters[key] = "\(value)" as AnyObject?
}
return dictParameters
}
然后将Alamofire称为
Alamofire.request(.POST, "http://server.com", parameters: prepareforObject(parameters), encoding: .JSON)
.responseJSON { request, response, JSON, error in
print(response)
print(JSON)
print(error)
}