0

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?

7
  • The param should be a dictionary do not make it with struct. Commented Jan 22, 2018 at 7:07
  • Request will be [String:[[String,Any]]] Commented Jan 22, 2018 at 7:08
  • @SalmanGhumsani, an array of dictionaries? Each dictionary containing the id's and the comment? Commented Jan 22, 2018 at 7:28
  • Yes absolutely. Commented Jan 22, 2018 at 7:29
  • 1
    @SalmanGhumsani, thank you very much , it worked. But I had to use [String : [[String:AnyObject]]] instead. Commented Jan 22, 2018 at 8:32

1 Answer 1

1

The 'param' should be a dictionary do not make it with struct.

The request body data type is: [String:[[String,Any]]]

Sign up to request clarification or add additional context in comments.

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.