var parameters = [
"userName": userName,
"password": passwordSalt,
"somethingElse": somethingElse
]
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters , options: nil, error: &err)
到这里为止都很简单,现在我遇到了问题。我正在尝试使用Alamofire Post来发布多个JSON对象,它们应该是这样的:
[
{
"comment": "my First Comment",
"commentDate": "2014-05-13 14:30 PM",
"isSigned": 1,
"patientId": 2,
"documentId": 3
},
{
"comment": "my SecondComment",
"commentDate": "2014-05-14 14:30 PM",
"isSigned": 2,
"patientId": 3,
"documentId": 4
},
{
"comment": "my third Comment",
"commentDate": "2014-05-15 14:30 PM",
"isSigned": 3,
"patientId": 4,
"documentId": 5
}
]
我如何通过迭代JSON对象来创建上述数组/ JSON(我不确定该怎么称呼它)?我知道如何从JSON对象中获取JSON值,我想知道如何创建像上面示例中那样的参数变量来保存数据。使用Alamofire是否可以做到这一点?(一次POST多个对象)
我尝试过几种方法,但都没有成功。
var dictArray = [Dictionary<String, Any>] var dict = Dictionary<String, Any>
While iterating over JSON object inserted each value in dict and appended dict to dictArray, now when I'm trying to use dictArray as parameters in .dataWithJSONObject it doesn't like the object.
var dict = Dictionary<String, AnyObject> var array = NSArray()
extracted each value by iterating over the JSON object and inserted them into dict and tried inserting dict into array. But this gives a different problem. The way it builds the objects is different from what is required, as follows.
[ { comment: my First Comment, commentDate: 2015-05-13 13:30 PM"", isSigned: 1, patientId: 2, documentId: 3 }, { comment: my Second Comment, commentDate: 2015-05-13 13:30 PM"", isSigned: 2, patientId: 5, documentId: 4 }, { comment: my third Comment, commentDate: 2015-06-13 13:30 PM"", isSigned: 5, patientId: 1, documentId: 9 } ]
Here the Keys doesn't get wrapped inside quotes (Correct way: "comment", wrong way: comment).
有人尝试过发布多个对象吗?alamofire能够做到吗?希望我的问题表述清楚了。如果这个问题太简单了,请见谅,我花了一整天的时间来解决它,但是没有成功。