1

Let's say I got the following struct

public class Response: Codable {
    let status: String
    let code: String
    let id: String
}

What I want is to get the class properties and values as [String: Any] to send it through Alamofire like this:

let response: Response = Response(status: "A", code: "B", uuid: "C")
let data = try JSONEncoder().encode(res)

//Data to [String : Any]

Alamofire.request("endpoint", method: .post, parameters: params).responseJSON {
    // Handle response
}
Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131

2 Answers2

1

You can use something like this:

let response: Response = Response(status: "A", code: "B", uuid: "C")
let data = try JSONEncoder().encode(res)

//Data to [String : Any]
do {
    let params = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
    Alamofire.request("endpoint", method: .post, parameters: params).responseJSON {
        // Handle response
    }
} catch {
    print(error)
}
Mukesh
  • 2,792
  • 15
  • 32
-1

Try using JSONSerialization as below I had used to get data from JSON

func HitApi(){
    Alamofire.request(urlToGetTimeTable, method: .get, parameters: nil , encoding:URLEncoding.default).responseJSON { (response) in

        if(response.result.isSuccess)
        {
            if let JSON = response.result.value
            {
                print("JSON: \(JSON)")
                do {
                        //Clearing values in Array
                        self.subjectNameArray.removeAll()
                        //get data and serialise here to get [String:Any]
                        if let data = response.data,
                            let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
                            let dataDict = json["data"] as? [[String: Any]]

                        {
                            // iterate an array
                            for dict in dataDict
                            {
                                //get data from JSON Response
                                let subjectName = dict["subjects_id"] as? String
                                self.subjectNameArray.append(subjectName!)
                            }
                            // TableView Delegate & DataSource
                            // Reload TableView
                            self.tableView.dataSource = self;
                            self.tableView.delegate = self;
                            self.tableView.reloadData()

                        }

                }
                catch
                {
                    //Error case
                    print("Error deserializing JSON: \(error)")

                }

            }
        }

        if(response.result.isFailure)
        {
            //Show Alert here //reason Failure
        }


    }
}

Give you an idea to get response as [String:Any] using son serialisation , you can use Above format in Post Method need some Modification. I deleted Rest code and showed main Code that was required

iOS Geek
  • 4,825
  • 1
  • 9
  • 30
  • You misunderstood me, what I want is to convert JSONEncoder().encode(mystruct) into [String:Any] to send it as paratemers for an Alamofire request – Jonathan Solorzano Jan 09 '18 at 05:32