DigiProTyler
DigiProTyler

Reputation: 11

How do I format this data using Swift so the back end can read it?

I'm trying to format my data so that my back end can read it, but I feel like I'm missing something super simple.

Here's how the data should look:

{
    "month": 12,
    "year": 2012,
    "status": "Submitted",
    "expenses": [
        {
            "name": "MORTGAGE"
        },
        {
            "name": "HOTEL"
        }
    ]
}

And based on the things I've been trying from current Stack Overflow questions and other resources, here's what I've currently got as output (formatted here for easy reading):

[
  "year": 2018,
  "status": "Not Submitted",
  "month": 3,
  "expenses": [
    ["name": "MORTGAGE"],
    ["name": "HOTEL"]
  ]
]

I've got a data object which stores the data and then converts all the values to [String:Any].

var expenseObject = DocumentData_UploadData()
expenseObject.month = month
expenseObject.year = year
expenseObject.expenses = expenses
expenseObject.status = "Not Submitted"

let parameters = expenseObject.convertToDictionary()
print(parameters)

Alamofire.request(APIEndPoints.document_Create, method: .post, parameters: parameters, headers: headers).responseJSON

Here's the data struct:

struct DocumentData_UploadData {

    var month = 0
    var year = 0
    var status = ""
    var expenses: [String] = []

    func convertToDictionary() -> [String: Any] {
        var expenseDict: [[String:Any]] = []
        for expense in expenses {
            expenseDict.append(["name":expense])
        }
        let dict: [String:Any] = [
            "month": month,
            "year": year,
            "status": status,
            "expenses": expenseDict
        ]
        return dict
    }

}

The server can successfully read the month, year, and status values in the data I'm sending, but it isn't able to read the expense values, and that's where I'm getting hung up. I've tried a few different methods, but seem to always get the same rejection from the server complaining about the format of the expense data. Thanks!

Upvotes: 0

Views: 56

Answers (1)

DigiProTyler
DigiProTyler

Reputation: 11

It indeed was something very simple: I needed to go back to a standard [String:Any] format for the parameters and then add JSONEncoding.default to the encoding value of the Alamofire request. I ended up not needing the DocumentData_UploadData class at all.

Here's my final code:

static func document_Create(month: Int, year: Int, expenses: [String], completion: @escaping (APIResponses, String?, DocumentData?) -> Void) {
    let headers = [
        "Authorization":getBearerAuthorization()
    ]

    let expensesDictionary: [[String:Any]] = expenses.map({ ["name": $0] })

    let parameters = [
        "month": month,
        "year": year,
        "status": "Not Submitted",
        "expenses": expensesDictionary
    ] as [String : Any]

    Alamofire.request(APIEndPoints.document_Create, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON {
        response in

        // Handle response
    }
}

Upvotes: 1

Related Questions