Swift 4 JSON Codable - 返回值有时是对象,有时是数组

9

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


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


是否有一种方法可以创建一个考虑到这一点并且能够分配正确类型来转换值('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
    }
}

这些结果是随机排列的,也就是说我不知道什么时候会收到一个对象或数组。
这里是完整的游乐场文件:
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)
1个回答

11

你可以使用带有关联值的枚举(在这种情况下为字符串和数组)来封装结果的歧义性,例如:

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
    }
}

嗨@andrea-mugnaini,感谢您提供的解决方案。您能推荐一些资源(书籍、网站、论坛)来涵盖/教授更多关于这种“非标准”问题的知识吗?我是一个新手,非常感谢任何可以让我更好地理解如何得出这些解决方案的信息。谢谢。 - Andres G
@mugx 我该如何使用它?你没有给出如何使用的示例,它无法识别为字符串或数组,我不知道如何获取它的值并在我的应用程序中显示。 - Basel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接