How to use JSON arrays with Alamofire parameters?

I am having problems structuring my parameters so that our server API can read it as valid JSON.

Alamofire uses such options in a fast language

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

The problem is that AnyObject doesn't seem to accept JSON, so how could I send / create such a structure with speed?

 { "string": str, "params" : [ { "param1" : "something", "param2" : 1, "param3" : 2, "param" : false }, { "param1" : "something", "param2" : 1, "param3" : 2, "param" : false }] } 
+5
source share
6 answers

Adapted from Alamofire 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 ] ] ] 
+5
source

I decided it myself. I just can do

  parameters = [ "params": array ] 

Where array is Dictionary (String, AnyObject). The problem that I first came across with this solution was that you cannot insert booleans into this dictionary, they just convert to integers. But apparently the JSON encoding alamofire (I think) sends them as true / false values.

+1
source

You need to create an NSArray object for the array parameters.

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

if you use 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) 
0
source

Swift 2.2 and using SwiftyJSON.swift
You can use 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 ] 
0
source

If necessary, pass the array directly as a parameter to the 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 { 
0
source

All Articles