9

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 language

let parameters : [String: AnyObject] =
[
    "string": str
    "params": HOW I INSERT A VALID JSON ARRAY HERE
]

The problem is that AnyObject does not seem to accept JSON so how would I send / create a structure like this with swift?

{
"string": str, "params" : [
    {
        "param1" : "something",
        "param2" : 1,
        "param3" : 2,
        "param" : false
    },
    {
        "param1" : "something",
        "param2" : 1,
        "param3" : 2,
        "param" : false
    }]
}
1
  • 2
    What you're calling a "JSON array" here is just an array of dictionaries. It's not JSON until Alamofire encodes it into JSON for the request. Commented May 22, 2015 at 18:17

7 Answers 7

5

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
]
]
]
Sign up to request clarification or add additional context in comments.

3 Comments

This does not exactly answer my question. It's not like i'm going to hard code the JSON into the parameters. Bar in that example is also a Dictionary that AnyObject does accept, not a JSON array.
For the edit: Correct, sort of but I still need to insert the "params" as an object, not a hard coded structure.
@MiikaPakarinen Whether the value is hard-coded or constructed in some other way is irrelevant.
1

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.

Comments

1

In case, there is a need to pass array directly as a parameter for a alamofire request, the following method worked for me.

source: https://github.com/Alamofire/Alamofire/issues/1508

let headers = NetworkManager.sharedInstance.headers
var urlRequest = URLRequest(url: URL(string: (ServerURL + Api))!)
urlRequest.httpMethod = "post"
urlRequest.allHTTPHeaderFields = headers
let jsonArrayencoding = JSONDocumentArrayEncoding(array: documents)
let jsonAryEncodedRequest = try? jsonArrayencoding.encode(urlRequest, with: nil)

    var request: Alamofire.DataRequest? = customAlamofireManager.request(jsonAryEncodedRequest!)
    request?.validate{request, response, data in
        return .success
        }
        .responseJSON {

Comments

0

You need to create a NSArray object for array parameters.

var yourParameters = [
  "String": "a string",
  "Int": 1,
  "Array": NSArray(
    array: [
        "a", "b", "c"
    ])
]

Comments

0

if you are using SwiftyJSON, you can write like this

let array = ["2010-12-13T5:03:20","2010-12-13T5:03:20"]
 let paramsJSON = JSON(array)
 var arrString = paramsJSON.rawString(NSUTF8StringEncoding)

Comments

0

Swift 2.2 and using SwiftyJSON.swift
You can use like this.

    var arrayList : [String: AnyObject]//one item of array

    var list: [JSON] = []//data array 

    for i in 0..<10//loop if you need
    { 

        arrayList = [
            "param1":"",
            "param1":"",
            "param2":["","",""]
        ]

        list.append(JSON(arrayList))//append to your list

    }


    //params
    let params: [String : AnyObject]=[

        "Id":"3456291",
        "List":"\(list)"//set
    ]

Comments

0

Using Swift 5 You can use like this.

  var authParams:[String:Any] = [:]

  var authParamsObject:[String:Any] = [:]


    authParamsObject["is_urgent"] = data_any
    authParamsObject["body"] = data_any

  authParams = ["note_set" : authParamsObject]

Json Result :

{
  "note_set":
    {
      "is_urgent": true,
      "body": "string"
    }
}

1 Comment

Not getting the output like that comes with [] bracket

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.