How do I use JSON arrays with Alamofire parameters?

前端 未结 8 1908
借酒劲吻你
借酒劲吻你 2021-01-05 01:03

I\'m having a bit of trouble structuring my parameters so that our server API would be able to read it as valid JSON.

Alamofire uses parameters like this in swift la

相关标签:
8条回答
  • 2021-01-05 01:30

    Solved this myself. I can just do

        parameters =
        [
            "params": array
        ]
    

    Where array is Dictionary (String, AnyObject). The problem I initially had with this solution was that you can't insert booleans into this kind of dictionary, they will just be converted into integers. But apparently alamofire JSON encoding (I think) sends them as true/false values nevertheless.

    0 讨论(0)
  • 2021-01-05 01:31

    Taken from Alamofire's GitHub page:

    let parameters = [
        "foo": [1,2,3],
    "bar": [
        "baz": "qux"
    ]
    ]
    
    Alamofire.request(.POST, "http://httpbin.org/post", parameters: parameters, encoding: .JSON)
    // HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}
    

    EDIT: And from your example:

    let parameters = [
        "string": "str",
    "params": [[
        "param1" : "something",
        "param2" : 1,
        "param3" : 2,
        "param" : false
    ],[
        "param1" : "something",
        "param2" : 1,
        "param3" : 2,
        "param" : false
    ]
    ]
    ]
    
    0 讨论(0)
提交回复
热议问题