在Alamofire POST方法中POST多个json对象 - Swift/IOS

Sas*_*shi 7 json ios swift alamofire

对不起,如果我的问题不明确,我会尽量让自己明白解释.所以这正是我想要做的,我正在尝试使用Alamofire发布多个评论(我的应用程序实现的东西,并且每当用户编写新评论时都将存储为JSON对象).我将这些JSON注释传递给我的post例程,在那里我可以使用SwiftyJSON来提取每个值.Noe,我知道如何设置参数,如果我试图授权用户如下,

    var parameters = [
    "userName": userName,
    "password": passwordSalt,
    "somethingElse": somethingElse
    ]
    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters , options: nil, error: &err)
Run Code Online (Sandbox Code Playgroud)

直到这里,这很简单,现在是我的问题.我正在尝试使用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
    }
 ]
Run Code Online (Sandbox Code Playgroud)

如何通过迭代JSON对象来创建上面的数组/ json(我不确定要调用它的内容)?我知道如何从JSON对象获取JSON值我要问的是如何创建此参数变量来保存数据,如上例所示.使用Alamofire甚至可以做到这一点吗?(一次POST多个对象)

我尝试了几种方法,但他们没有成功

  1. var dictArray = [Dictionary<String, Any>]
    var dict = Dictionary<String, Any>
    
    Run Code Online (Sandbox Code Playgroud)

    虽然遍历JSON对象插入每个值字典和附加字典dictArray,现在当我试图使用dictArray作为.dataWithJSONObject它不喜欢的对象参数.

  2. var dict = Dictionary<String, AnyObject>
    var array = NSArray()
    
    Run Code Online (Sandbox Code Playgroud)

    通过遍历JSON对象并将它们插入到dict中并尝试将dict插入到数组中来提取每个值.但这给出了一个不同的问题.它构建对象的方式与所需的不同,如下所示.

    [
       {
        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 
       }
    ]
    
    Run Code Online (Sandbox Code Playgroud)

    这里的不会被包含在引号内(正确的方式:"注释",错误的方式:注释).

有没有人尝试发布多个对象,alamofire是否能够这样做?我希望我明白这个问题.对不起,如果这个问题太简单无法回答,我花了一整天的时间来解决这个问题,但没有成功.

Ene*_*nso 5

Swift中您为已发布的评论对象数组的正确表示将如下所示:

    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
        ]
    ]
Run Code Online (Sandbox Code Playgroud)

发送单个评论非常简单:

    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)
    }
Run Code Online (Sandbox Code Playgroud)

但是,为了发送一系列评论,您似乎必须生成URLRequest您自己,然后将其传递给Alamofire,如下所示:

    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)
    }
Run Code Online (Sandbox Code Playgroud)

如果您可以修改API后端以接受具有多个注释的对象,您也可以通过以下方式发送它们:

    Alamofire.request(.POST, "http://httpbin.org/post", parameters: ["comments": comments]).responseJSON { (req, res, json, error) in
        println(req)
        println(res)
        println(json)
        println(error)
    }
Run Code Online (Sandbox Code Playgroud)

问候.