Here is the JSON I need to post:
{
"results": [
{
"case_id": 1,
"status_id": 5,
"comment": "This test failed"
},
{
"case_id": 2,
"status_id": 1,
"comment": "This test passed",
},
..
{
"case_id": 1,
"assignedto_id": 5,
"comment": "Assigned this test to Joe"
}
..
]
}
What I've tried doing is this :
let parameters = [
"results" : data
] as [String : Any]
let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])
Here, data is an array of structs of the type (data:[param]) :
struct param {
var status_id: Int
var case_id: String
var comment: String
}
The code fails at JSONSerialization. It enters the catch() block. I tried giving the parameters like this and it worked,
let parameters = [ "results" : [
[
"case_id": "20275",
"status_id": 5,
"comment": "This test failed"
],
[
"case_id": "20276",
"status_id": 1,
"comment": "This test passed",
],
]] as [String : Any]
How can I reproduce this structure? Because I can't hard code the values of status id's and case id's. I store the status id's and case id's in arrays.I thought creating an array of structs and substituting the id values will be enough, unfortunately it's not working. How do I post the data then?
[String:[[String,Any]]]