如何使用Swift 4.2正确编码JSON响应?

时间:2019-02-18 06:40:28

标签: json swift

我的情况是,我正在尝试为codable响应创建JSON结构,但出现“Type 'Datum' does not conform to protocol 'Decodable' “错误。我在codable中犯了一些错误,但找不到。请check下方的response并进行编码,然后为我提供解决方案。

我的回应

{
    "status": 200,
    "message": "Success",
    "country": [
        {
            "id": 1,
            "master": "India",
            "type": 2,
            "active": 1
        },
        {
            "id": 2,
            "master": "US",
            "type": 2,
            "active": 1
        }
    ],
    "cost": 13764,
    "data": [
        {
            "id": 1,
            "user_id": 167,
            "country": 1,
            "card": 1,
            "category": 4,
            "title": “kms”,
            "description": “check”,
            "cost": 444,
            "attachment": "sample.png",
            "create_date": "2019-02-13T00:00:00.000Z",
            "device_id": "1111",
            "app_type": "Android",
            "location": “USA”,
            "user": {
                "firstname": “Mike”,
                "lastname": "P"
            },
            "app_trans_card": {
                "card": “012”
            },
            "app_trans_master": {
                "master": "Domain"
            }
        }
   ]
}

我的代码

struct Root: Codable {
    let status: Int
    let message: String
    let cost: Int
    let data: [Datum]

    enum CodingKeys: String, CodingKey {
        case status
        case message = "message"
        case cost
        case data
    }
}

struct Datum: Codable {
    let id, user_id, country, card, category, cost: Int
    let title: String
    let description: String
    let attachment: String
    let create_date: String
    let device_id: String
    let app_type: String
    let location: String

    let user: Fullname
    let app_trans_card: TransactionCard
    let app_trans_master: TransactionMaster

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case user_id = "user_id"
        case country = "country"
        case card = "card"
        case category = "category"
        case cost = "cost"
        case title, description, attachment, create_date, device_id, app_type, location, fullname, transactionCard, transactionMaster
    }
}

struct Fullname: Codable {
    let firstname, lastname: String
}

struct TransactionCard: Codable {
    let card: String
}

struct TransactionMaster: Codable {
    let master: String
}

1 个答案:

答案 0 :(得分:1)

如果您在许多地方都与JSON相关,我发现有些问题,应该是",所以请先更正,然后您的JSON

{
    "status": 200,
    "message": "Success",
    "country": [
        {
            "id": 1,
            "master": "India",
            "type": 2,
            "active": 1
        },
        {
            "id": 2,
            "master": "US",
            "type": 2,
            "active": 1
        }
    ],
    "cost": 13764,
    "data": [
        {
            "id": 1,
            "user_id": 167,
            "country": 1,
            "card": 1,
            "category": 4,
            "title": "kms",
            "description": "check",
            "cost": 444,
            "attachment": "sample.png",
            "create_date": "2019-02-13T00:00:00.000Z",
            "device_id": "1111",
            "app_type": "Android",
            "location": "USA",
            "user": {
                "firstname": "Mike",
                "lastname": "P"
            },
            "app_trans_card": {
                "card": "012"
            },
            "app_trans_master": {
                "master": "Domain"
            }
        }
   ]
}

现在,您可以使用THIS网站创建Codable协议,并对其进行编码,如下所示:

struct Root: Codable {
    let status: Int
    let message: String
    let country: [Country]
    let cost: Double
    let data: [Datum]
}

struct Country: Codable {
    let id: Int
    let master: String
    let type, active: Int
}

struct Datum: Codable {
    let id, userID, country, card: Int
    let category: Int
    let title, description: String
    let cost: Double
    let attachment, createDate, deviceID, appType: String
    let location: String
    let user: User
    let appTransCard: AppTransCard
    let appTransMaster: AppTransMaster

    enum CodingKeys: String, CodingKey {
        case id
        case userID = "user_id"
        case country, card, category, title, description, cost, attachment
        case createDate = "create_date"
        case deviceID = "device_id"
        case appType = "app_type"
        case location, user
        case appTransCard = "app_trans_card"
        case appTransMaster = "app_trans_master"
    }
}

struct AppTransCard: Codable {
    let card: String
}

struct AppTransMaster: Codable {
    let master: String
}

struct User: Codable {
    let firstname, lastname: String
}