这是我从 Postman 那里得到的Post API响应,检查表完成有两个关键“checklist_done”,“work_with_price”
{
"additional_data": {
"id": 215,
"booking_id": 221,
"checklist_done": "{\"4\":\"Gear Oil\",\"2\":\"Engine Oil Change\",\"3\":\"Air and Oil Filter\"}",
"work_with_price": "{\"chain\":\"145\",\"Battery\":\"58\"}",
"work_price": "203",
"estimated_price": 149,
"discount_code": "admin",
"mechanic_profit": "200",
"discount": 0,
"ccavenue_charge": 0,
"ccavenue_gst": 0,
"total_price": "352",
"payment_method": null,
"gateway": null,
"payment_response": null,
"status": "pending",
"paymentmode": null,
"bankname": null,
"txnid": null,
"banktxnid": null,
"txndate": null,
"created_at": "2023-04-27T18:20:07.000000Z",
"updated_at": "2023-04-27T18:27:19.000000Z",
"admin_discount": 0
}
}
我已经创建了如下模型,但JsonDecoder无法将响应Map到我创建的模型,我将“checklistDone”和workWithPrice标记为Stein?
struct AdditionalData: Codable {
var id, bookingID: Int?
var checklistDone: String?
var workWithPrice: String?
var workPrice: String?
var estimatedPrice: Int?
var discountCode, mechanicProfit: String?
var discount, ccavenueCharge, ccavenueGst: Int?
var totalPrice: String?
var paymentMethod, gateway, paymentResponse: String?
var status: String?
var paymentmode, bankname: String?
var txnid, banktxnid: String?
var txndate: String?
var createdAt, updatedAt: String?
var adminDiscount: Int?
enum CodingKeys: String, CodingKey {
case id
case bookingID = "booking_id"
case checklistDone = "checklist_done"
case workWithPrice = "work_with_price"
case workPrice = "work_price"
case estimatedPrice = "estimated_price"
case discountCode = "discount_code"
case mechanicProfit = "mechanic_profit"
case discount
case ccavenueCharge = "ccavenue_charge"
case ccavenueGst = "ccavenue_gst"
case totalPrice = "total_price"
case paymentMethod = "payment_method"
case gateway
case paymentResponse = "payment_response"
case status
case paymentmode, bankname, txnid, banktxnid, txndate
case createdAt = "created_at"
case updatedAt = "updated_at"
case adminDiscount = "admin_discount"
}
}
请指导我的模型,我已经创建是错误的或其他任何东西
1条答案
按热度按时间csbfibhn1#
我已经使用了下面的模型,我能够解码模型。
你解码的时候根对象应该是APIData,因为你上面AdditionalData的响应里面有一个根对象,见下面代码。