POST multiple json objects in Alamofire POST method - Swift/IOS -


sorry if question not clear, i'l try make myself clear explanation. here i'm trying do, i'm trying use alamofire post more 1 comment (something app implements , stored json object whenever user writes new comment). i'm passing these json comments post routine, can use swiftyjson extract each value. noe thing know how set parameters if i'm trying authorize user follows,

    var parameters = [     "username": username,     "password": passwordsalt,     "somethingelse": somethingelse     ]     var err: nserror?     request.httpbody = nsjsonserialization.datawithjsonobject(parameters , options: nil, error: &err) 

it's quite straighforward until here, comes problem. i'm trying use alamofire post post multiple json objects, should this

[    {     "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     }  ] 

how create above array/json (i'm not sure on call this) iterating json object. i know how json values json object i'm asking how create parameters variable hold data above example. possible using alamofire? (post multiple objects @ once)

i tried couple of ways didn't work out

  1. var dictarray = [dictionary] var dict = dictionary

    while iterating on json object inserted each value in dict , appended dict dictarray, when i'm trying use dictarray parameters in .datawithjsonobject doesn't object.

  2. var dict = dictionary var array = nsarray()

    extracted each value iterating on json object , inserted them dict , tried inserting dict array. gives different problem. way builds objects different required, follows.

    [ { comment: first comment, commentdate: 2015-05-13 13:30 pm"", issigned: 1, patientid: 2, documentid: 3 }, { comment: second comment, commentdate: 2015-05-13 13:30 pm"", issigned: 2, patientid: 5, documentid: 4 }, { comment: third comment, commentdate: 2015-06-13 13:30 pm"", issigned: 5, patientid: 1, documentid: 9 } ]

    here keys doesn't wrapped inside quotes (correct way: "comment", wrong way: comment).

did try posting multiple objects, alamofire capable of doing so? hope made question clear. sorry if simple of question answer, spent whole day figuring out, didn't work out. thank you

the correct representation in swift array of comment objects have posted this:

    let comments: array<[string:anyobject]> = [         [             "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         ]     ] 

sending single comment simple:

    let comment: [string:anyobject] = [         "comment": "my first comment",         "commentdate": "2014-05-13 14:30 pm",         "issigned": 1,         "patientid": 2,         "documentid": 3     ]      alamofire.request(.post, "http://httpbin.org/post", parameters: comment).responsejson { (req, res, json, error) in         println(req)         println(res)         println(json)         println(error)     } 

however, in order send array of comments, seems have generate urlrequest self , pass alamofire follows:

    let mutableurlrequest = nsmutableurlrequest(url: nsurl(string: "http://httpbin.org/post")!)     mutableurlrequest.httpmethod = "post"     var error: nserror? = nil      let options = nsjsonwritingoptions.allzeros     if let data = nsjsonserialization.datawithjsonobject(comments, options: options, error: &error) {         mutableurlrequest.setvalue("application/json", forhttpheaderfield: "content-type")         mutableurlrequest.httpbody = data     }      alamofire.request(mutableurlrequest).responsejson { (req, res, json, error) in         println(req)         println(res)         println(json)         println(error)     } 

if modify api backend accept object multiple comments, send them way:

    alamofire.request(.post, "http://httpbin.org/post", parameters: ["comments": comments]).responsejson { (req, res, json, error) in         println(req)         println(res)         println(json)         println(error)     } 

regards.