如何将嵌套对象作为参数发送?

时间:2017-08-09 07:15:35

标签: ios swift alamofire

我有一个json对象要发送。我应该如何在swift中将其作为帖子请求发送。使用alamofire或本地http帖子我不介意。

我的目标如下:

 public IHttpActionResult GetOrdersOfPostman(string postmanId)
    {
        try
        {
            var postmanCustomersModel = new List<PostmanCustomerModel>();
            var id = Convert.ToInt64(postmanId);

            var orderStatus = (byte) OrderStatus.DeliverToPostman;
            var orders = _orderRepository.AsQueryable().Where(x => x.PostmanId == id && x.OrderStatus==orderStatus &&
                         x.SellerCustomerId > 0).ToList();

            return Ok(orders);
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

我也尝试过alamofire,但为此我必须将其转换为字典。 我想将此对象作为身体参数发送,我该怎么做?

2 个答案:

答案 0 :(得分:1)

您应该尝试以下方法:

let parameters: [String: AnyObject] = [
"IdQuiz" : 102,
"IdUser" : "iosclient",
"User" : "iosclient",
"List": [
    [
        "IdQuestion" : 5,
        "IdProposition": 2,
        "Time" : 32
    ],
    [
        "IdQuestion" : 4,
        "IdProposition": 3,
        "Time" : 9
    ]
]
]

 var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")




request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)

Alamofire.request(request)
.responseJSON { response in
    // do whatever you want here
    switch response.result {
    case .failure(let error):
        print(error)

        if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
            print(responseString)
        }
    case .success(let responseObject):
        print(responseObject)
    }
}

答案 1 :(得分:1)

以下是上述json对象的示例代码:

do {
        let arrayStartLocation = [7.2916216, 80.6341326]
        let arrayEndLocation = [7.2916216, 80.6341326]
        let arrayNorthEast = [7.2916216, 80.6341326]
        let arraySouthWest = [7.2916216, 80.6341326]
        let dictBounds = ["NorthEast" : arrayNorthEast , "SouthWest": arraySouthWest]
        let dictRoute = ["Bounds" : dictBounds]
        let dictMain: [String : Any] = ["StartAddress": "Colombo",
                        "EndAddress": "Kandy",
                        "DepartureAddress": "Kollupitiya, Colombo",
                        "StartLocation": arrayStartLocation,
                        "EndLocation": arrayEndLocation,
                        "Route": dictRoute

        ///Similarly For remaining keys
        //    .......
        ]


        //Convert to Data
        let jsonData = try JSONSerialization.data(withJSONObject: dictMain, options: JSONSerialization.WritingOptions.prettyPrinted)

        //Convert back to string. Usually only do this for debugging

        if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
            print(JSONString)/// Send this string in almofire

            Alamofire.request(AppUrl.CALL_ADD_REVIEWS, method: .post , parameters: ["yourPramName": JSONString]).responseJSON(completionHandler: { (response) in
                //Code here
            })
        }
    } catch {
        print(error.localizedDescription)
    }