Swift 4 JSON Codable - 返回的值有时是一个对象,其他是一个数组

Swift 4 JSON Codable - value returned is sometimes an object, others an array

我从 API returns 单个对象中获取的数据,但是当有多个对象时,它 returns 是同一键中的数组。使用我正在使用的当前模型(结构),当数组出现时解码失败。

这些结果是随机排序的,这意味着我无法知道何时会为我提供对象或数组。


有没有一种方法可以创建一个考虑到这一事实的模型,并可以为值('String' 或“[String]”)分配正确的类型,以便继续解码而不会出现问题?


这是返回对象的示例:

{
    "firstFloor": {
        "room": "Single Bed"
    }
}

这是返回数组时的示例(对于相同的键):

{
    "firstFloor": {
        "room": ["Double Bed", "Coffee Machine", "TV", "Tub"]
    }
}

应该能够用作解码上述两个样本的模型的结构示例:

struct Hotel: Codable {
    let firstFloor: Room

    struct Room: Codable {
        var room: String // the type has to change to either array '[String]' or object 'String' depending on the returned results
    }
}

这些结果是随机排序的,这意味着我无法知道何时会为我提供对象或数组。

这是完整的 playground 文件:

import Foundation

// JSON with a single object
let jsonObject = """
{
    "firstFloor": {
        "room": "Single Bed"
    }
}
""".data(using: .utf8)!

// JSON with an array instead of a single object
let jsonArray = """
{
    "firstFloor": {
        "room": ["Double Bed", "Coffee Machine", "TV", "Tub"]
    }
}
""".data(using: .utf8)!

// Models
struct Hotel: Codable {
    let firstFloor: Room

    struct Room: Codable {
        var room: String // the type has to change to either array '[String]' or object 'String' depending on the results of the API
    }
}

// Decoding
let decoder = JSONDecoder()
let hotel = try decoder.decode(Hotel.self, from: jsonObject) //

print(hotel)

您可以使用具有关联值(在本例中为字符串和数组)的枚举来封装结果的歧义,例如:

enum MetadataType: Codable {
    case array([String])
    case string(String)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        do {
            self = try .array(container.decode(Array.self))
        } catch DecodingError.typeMismatch {
            do {
                self = try .string(container.decode(String.self))
            } catch DecodingError.typeMismatch {
                throw DecodingError.typeMismatch(MetadataType.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Encoded payload not of an expected type"))
            }
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .array(let array):
            try container.encode(array)
        case .string(let string):
            try container.encode(string)
        }
    }
}

struct Hotel: Codable {
    let firstFloor: Room

    struct Room: Codable {
        var room: MetadataType
    }
}