首页 > 解决方案 > 如何将有效负载转换为 formData

问题描述

有没有一种方法可以将有效负载 json 对象转换为 formData ?const formData: FormData = new FormData();请检查下面我的有效负载代码我已添加它

#有效负载输出

{
    "id": 0,
    "name": null,
    "dealType": "Partner Location Submission",
    "annualRentProposed": null,
    "annualRentCurrent": null,
    "firmTermRemaining": null,
    "firmTermAdded": null,
    "maxAvailableTerm": null,
    "status": null,
    "capitalContribution": null,
    "parentCloneId": null,
    "accountId": 4,
    "transactionId": 173,
    "dealTypeValues": "{\"id\":0,\"summary\":\"13123\",\"mlasId\":2,\"startDate\":\"2021-10-27\",\"endDate\":\"2021-10-27\",\"rent\":\"2321\",\"cam\":\"312\",\"securityMonitoringMonthly\":\"12312\",\"supportServicesFee\":\"312\",\"estimatedOtherRevenue\":\"12312\",\"descriptionOfOtherRevenue\":\"3123123\",\"totalMonthlyRentAndFees\":15380,\"buildOutCostReimbursement\":\"123123\",\"dealId\":0,\"startDateString\":\"2021-10-27\",\"endDateString\":\"2021-10-27\"}",
    "isReadyForApproval": false
}

#有效载荷代码

const payload = {
        "id": 0,
        "name": this.dealPLSFormFields.dealName,
        "dealType": "Partner Location Submission",
        "annualRentProposed": null,
        "annualRentCurrent": null,
        "firmTermRemaining": null,
        "firmTermAdded": null,
        "maxAvailableTerm": null,
        "status": null,
        "capitalContribution": null,
        "parentCloneId": null,
        "accountId": this.currentAccount.accountId,
        "transactionId": this.transactionData.id,
        "dealTypeValues": JSON.stringify(dealTypeValues),
        "isReadyForApproval": this.isReadyForApproval    
      }

标签: angulartypescript

解决方案


您可以使用映射对象键将对象转换为 FormData:

const myObj = {a: 1, b: 2};

const formData = new FormData();

Object.keys(myObj).forEach(key => {
    formData.append(key, myObj[key]);
});



推荐阅读