1

My JSON is:

[
  {
    "a": "1",
    "b": "2"
  }
]

let parameters: Parameters = [
                <here goes the JSON>
            ]


Alamofire.request(URL, method: .put, parameters: parameters, encoding: JSONEncoding.default, headers: headers).validate().responseJSON 

I know that the parameters are a dictionary of [String:AnyObject] but I need to pass it like in my first JSON

And I need to send this as the parameter, but I think I can only send an [String: AnyObject], or at least thats how the example works in the Alamofire migration guide, so my question is how to achieve this? I'm using Alamofire 4

1
  • What do you mean by send as a parameter? Are you trying to extract the values and use them as params in a function call? Commented May 3, 2017 at 17:23

2 Answers 2

0

I have used a library called SwiftyJSON and with that you can simply do:

     var jsonArray: JSON = [
     "array": [1, 2],
     "users": [
         [
             "id": 1,
             "info": [
             "name": "name1",
             "email": "email1"
          ],
          "nums": [123, 124, 125]
      ],
      [
        "id": 2,
        "info": [
            "name": "name2",
            "email": "email2"
        ],
        "nums": [12, 13, 14]
      ]
]]
Sign up to request clarification or add additional context in comments.

Comments

0

I solved it like this:

var request = URLRequest(url: URL(string: yourURL)!)
    request.httpBody = try! JSON(yourObject).rawData()
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    Alamofire.request(request).validate(statusCode: 200..<600).responseJSON { response in
        print("Response \(response)")
    }

JSON is a lib: SwiftJSON

Comments

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.