在Alamofire POST方法中POST多个json对象 - Swift / IOS [英] POST multiple json objects in Alamofire POST method - Swift/IOS

查看:197
本文介绍了在Alamofire POST方法中POST多个json对象 - Swift / IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Sorry if my question is not clear, i'l try to make myself clear with explanation. So here is exactly what i'm trying to do, i'm trying to use Alamofire to post more than one comment (Something that my app implements and will be stored as a JSON object whenever user writes a new comment). I'm passing these JSON comments to my post routine, where i can use SwiftyJSON to extract each value. Noe the thing is i know how to set the parameters if i'm trying to authorize the user as follows,

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



直到这里,这是非常直截了当的,现在是我的问题。我正在尝试使用alamofire帖子发布多个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多个对象)

How do i create above array/json (i'm not exactly sure on what to call this) by iterating JSON object. I know how to get the JSON values from the JSON object all i'm asking is how to create this parameters variable to hold the data like above example. Is it even possible to do this using Alamofire? (POST multiple objects at once)

我尝试了几种方法,但他们没有锻炼

I tried couple of ways to but they didn't work out


  1. var dictArray = [字典]
    var dict = Dictionary

  1. var dictArray = [Dictionary] var dict = Dictionary

迭代JSON对象时,在 dict 中插入每个值并将 dict 附加到 dictArray ,现在我正在尝试使用dictArray作为.dataWithJSONObject中的参数,它不喜欢该对象。

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
var array = NSArray()

var dict = Dictionary var array = NSArray()

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

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
},
{
评论:我的第二条评论,
commentDate:2015-05-13 13:30 PM,
is签名:2,
patientId:5,
documentId:4
},
{
评论:我的第三条评论,
commentDate:2015-06-13 13:30 PM,
签名:5,
patientId:1,
documentId:9
}
]

[ { 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吗?我希望我提出问题。对不起,如果这个问题太简单无法回答,我花了整整一天来解决这个问题,但没有成功。谢谢

Did anyone try posting multiple objects, is alamofire capable of doing so? I hope i made question clear. Sorry if this is too simple of a question to answer, i spent my whole day figuring this out, but didn't work out. Thank you

推荐答案

您发布的评论对象数组在Swift中的正确表示形式如下:

The correct representation in Swift for the array of comment objects you have posted would be like 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 a single comment would be fairly 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)
    }

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

However, in order to send an array of comments, seems like you would have to generate the URLRequest your self and then pass it to Alamofire as 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)
    }

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

If you could modify the API backend to accept an object with multiple comments, you could also send them this 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)
    }

问候。

这篇关于在Alamofire POST方法中POST多个json对象 - Swift / IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆