簡體   English   中英

Swift Codable Json 響應

[英]Swift Codable Json Response

我有以下來自 API 的 JSON 響應,

{
    "status_code": 1000,
    "data": [
        {
            "id": 3,
            "invoice_id": 100000,
            "user_id": 1000,
            "contact_number": "NA",
            "province": 0,
            "location": "100000",
            "total": 0,
            "confirm_code": 1234,
            "status": 0,
            "created_at": "2020-03-18 22:07:25",
            "updated_at": "2020-03-18 22:07:25"
        },
        {
            "id": 4,
            "invoice_id": 100000,
            "user_id": 1000,
            "contact_number": "NA",
            "province": 0,
            "location": "100000",
            "total": 0,
            "confirm_code": 1234,
            "status": 0,
            "created_at": "2020-03-18 22:10:40",
            "updated_at": "2020-03-18 22:10:40"
        },
        {
            "id": 5,
            "invoice_id": 100000,
            "user_id": 1000,
            "contact_number": "NA",
            "province": 0,
            "location": "100000",
            "total": 0,
            "confirm_code": 1234,
            "status": 0,
            "created_at": "2020-03-18 22:12:29",
            "updated_at": "2020-03-18 22:12:29"
        }
    ],
    "message": null
}

我的結構是,

struct Invoice: Codable {
    let statusCode: Int
    let data: [Datum]
    let message: String?

    enum CodingKeys: String, CodingKey {
        case statusCode
        case data, message
    }
}

struct Datum: Codable {
    let id, invoiceID, userID: Int
    let contactNumber: String
    let province: Int
    let location: String
    let total, confirmCode, status: Int
    let createdAt, updatedAt: String

    enum CodingKeys: String, CodingKey {
        case id
        case invoiceID
        case userID
        case contactNumber
        case province, location, total
        case confirmCode
        case status
        case createdAt
        case updatedAt
    }
}

在視圖控制器中,

override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "http://xxxxx/api/invoices/\(1000)")!
        var request = URLRequest(url: url)
        request.httpMethod = "get"
        let task = session.dataTask(with: request) { (data, response, error) in
            guard let data = data else { return }
            do {
                let jsonData = try JSONDecoder().decode([Invoice].self, from: data)
                print(jsonData)
            }
            catch let jsonerr {
                print("error serrializing error",jsonerr)
            }

        }
        task.resume()
    }

但我保持以下錯誤消息,

錯誤序列化錯誤 typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary 相反。",underlyingError: nil))

請我在這里缺少什么! 任何幫助都感激不盡

Codable結構將像

struct Invoice: Codable {
    let statusCode: Int
    let data: [Datum]
    let message: String?

    enum CodingKeys: String, CodingKey {
        case statusCode = "status_code"
        case data, message
    }
}

struct Datum: Codable {
    let id, invoiceID, userID: Int
    let contactNumber: String
    let province: Int
    let location: String
    let total, confirmCode, status: Int
    let createdAt, updatedAt: String

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case invoiceID = "invoice_id"
        case userID = "user_id"
        case contactNumber = "contact_number"
        case province, location, total
        case confirmCode = "confirm_code"
        case status
        case createdAt = "created_at"
        case updatedAt = "updated_at"
    }
}

並且還使用

let jsonData = try JSONDecoder().decode(Invoice.self, from: data)

代替

 let jsonData = try JSONDecoder().decode([Invoice].self, from: data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM