如何将数据发送到post请求参数后面的JSON数据?

How to send data into post request parameters for the following JSON data?

我在包含多个产品的页面上有产品 return 所需的复选框,当我按下复选框时,我想将所选产品的 ID 和数量存储在 post 我想要 send.But 的参数,我无法发送正确的数据。如何发送正确的数据?

{
 "reasonId" : "001",
 "cancel" : "true",
 "description": "" ,
 "items": [
    {
        "id": "874a8064-bebf-41c3-98a8-6ac39a54156a",
        "quantity" : 480
    },
    {
        "id": "d7af8722-58cb-4bd0-9927-47de44ba2e0b",
        "quantity" : 2
    },
    {
        "id": "f799d66e-cfcd-4f2b-9603-1facf2fedbea",
        "quantity" : 1
    },
    {
        "id": "5ea0c31f-952a-4fbc-b623-9086030193ad",
        "quantity" : 17
    }
]
}

我可以打印所选产品的编号和数量。

private func createCheckBox(_ model : OrderDetailItems, tag: Int) -> UIView {
let rootView = UIView()
returnItemCount = model.definition?.count
var checkBox: ViewCheckLabel!
checkBox = ViewCheckLabel(text: "",
                            range: "",
                            action: {
    // CHECKK SELECT OR NOT SELECT
    checkBox.buttonCheck.checkboxAnimation {
        if checkBox.buttonCheck.isSelected {
            print(model.quantity)
            print(model.id)
        } else {
            
        }
    }
})

这是我的post请求

  func cancelOrderApi() {
        if let parentVC = parentViewController as? MyProfileViewController {
            parentVC.startActivityIndicator(mainView: parentVC.view)
            
                        let parameters : Parameters = [
                            "reasonId" : reasonId,
                            "cancel" : isOrdrReturnable,
                            "description": "",
                            "items" : ""
                        ]
            
            NetworkLayer.request(Router.returnOrder(parameters, orderReturnId ?? "")).responseDecodable(of: OrderCancel.self) { (response) in
                            
                            switch response.result {
                                case .failure(let error):
                                    Logger.shared.debugPrint("Error while fetching tags: \(String(describing: error))")
                                    return
                                case .success(let response):
                                    if response.code == 200 {
                                        parentVC.showAlert(mesg: response.message ?? "Siparişiniz İade edilmiştir", title: "Başarılı")
                                    } else {
                                        parentVC.showAlert(mesg: response.message ?? "", title: "Hata")
                                        Logger.shared.debugPrint(response.message ?? "")
                                    }
                                    
                                    Logger.shared.debugPrint(response)
                            }
                            DispatchQueue.main.async {
                                parentVC.stopActivityIndicator()
                                parentVC.profileTableView.reloadData()
                            }
                        }
                    }
    }

我不完全确定你想要实现什么,因为问题不清楚,但是这里有一个提示如何创建 Data 对象(稍后可以插入到 Url 请求中)你的 JSON 文件。首先手动将你的 JSON 文件复制到你的项目的某个地方并给它命名 yourJSONFile ,然后你可以使用 create createDataFromJson() 函数创建 Data 对象。

func createDataFromJson() -> Data? {
    if let path = Bundle.main.path(forResource: "yourJSONFile", ofType: "json") {
        do{
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
            return data
        } catch {
            print("catched : \(error.localizedDescription) ❌")
            return nil
        }
    } else {
        return nil
    }
}