在swift中发送参数中的多个数组

时间:2018-05-03 16:50:14

标签: ios json swift alamofire

我正在调用一个服务,我正在尝试在数组中发送由数组组成的参数以及数组外部的其他参数。我必须发送参数的格式是:

{
"RestID": 0,
"cart": [
{
  "childs": [
    {
      "addon_cat_id": 0,
      "id": 0,
      "name": "string",
      "next_move_id": 0,
      "price": 0,
      "sort_order": 0,
      "type": "string"
    }
  ],
  "name": "string",
  "price": "string",
  "productid": 0,
  "qty": 0,
  "description": "string"
}
],
"coupon_code": "string",
"coupon_type": "string",
"coupon_value": 0,
"delivery_price": "string",
"discount_amount": "string"
}

这就是我在服务中发送参数的方式:

 let parameter:[String:Any] = ["RestID":restaurantId!,
                                  "cart":[["childs":["addon_cat_id":"0",

                                                     "id":ItemDataSource.sharedInstance.items[0].itemId!,
                                                     "name":ItemDataSource.sharedInstance.items[0].itemName!,
                                                     "next_move_id":"",
                                                     "price":ItemDataSource.sharedInstance.items[0].itemPrice!,
                                                     "sort_order":"",
                                                     "type":"string"

                                    ]],
                                          ["name": ItemDataSource.sharedInstance.items[0].itemName!,
                                            "price": ItemDataSource.sharedInstance.items[0].itemPrice!,
                                            "productid": ItemDataSource.sharedInstance.items[0].itemId!,
                                            "qty": 2,
                                            "description":ItemDataSource.sharedInstance.items[0].itemDdescription!]
                                        ],
                                  "coupon_code" : couponCode!,
                                  "coupon_type" : couponType!,
                                  "coupon_value" : couponValue!,
                                  "delivery_price" : deliveryLbl.text!,
                                  "discount_amount" : disocunt!,
                                  "discount_description" : discountDesc!,
                                  "discount_info" : discountInfo!,
                                  "distance": "",
                                  "door_num" : doorTxt.text!,
                                  "firstname" : "",
                                  "lastname" : "",
                                  "order_type" : "Delivery",
                                  "payment_mode" : "",
                                  "phone" : mobileTxt.text!,
                                  "postcode" : postCodeTxt.text!,
                                  "preorder" : true,
                                  "preorder_is_preorder" : "",
                                  "street" : address1Txt.text!,
                                  "token" : "",
                                  "town" : addressTxt2.text!,
                                  "total": self.grandTotalLbl.text!,
                                  "stripeToken" : "",
                                  "customer_id" : "2",
                                  "preordertime": preOrderTime,
                                  "usercmt": "descri",
                                  "email": userEmail!
                                     ]

但是当我打电话给服务时,我收到了一个错误。我已经与服务器端讨论了这个问题,他们说发送参数是错误的。如何在参数?

中的数组中发送多个数组

这是我的服务类:

class SaveOrderDeliveryService{
    static let instance = SaveOrderDeliveryService()
    var status:Int = 0

    func saveOrderDelivery(param:[String:Any],completion:@escaping CompletionHandler)  {
        Alamofire.request(saveOrderDeliveryUrl, method: .post, parameters:param, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
            print("Request: \(String(describing: response.request))")   // original url request
            print("Response: \(String(describing: response.response))") // http url response
            print("Result: \(response.result)")// response serialization result
            print(response)

            if response.result.error == nil{
                self.status = (response.response?.statusCode)!
                if(self.status == 200){
                    print(response)
                    guard let data = response.data else {return}
                    do{
                        if let json = try JSON(data: data).dictionary{
                            completion(true)
                        }
                    }catch let jsonErr{
                        print(jsonErr)
                    }
                }

                completion(true)
            }else{
                completion(false)
                debugPrint(response.result.error as Any)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试添加您的数据,现在确保使用nil

    let parameters = [
        "RestID": "",
        "cart": [
            [
                "childs": [
                    [
                        "addon_cat_id": "",
                        "id": "",
                        "name": "string",
                        "next_move_id": "",
                        "price": "",
                        "sort_order": "",
                        "type": "string"
                    ]
                ],
                "name": "string",
                "price": "string",
                "productid": "",
                "qty": "",
                "description": "string"
            ]
        ],
        "coupon_code": "string",
        "coupon_type": "string",
        "coupon_value": "",
        "delivery_price": "string",
        "discount_amount": "string"
        ] as [String : Any]


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

    }