【问题标题】:Swift 4 JSON Codable - value returned is sometimes an object, others an arraySwift 4 JSON Codable - 返回的值有时是一个对象,有时是一个数组
【发布时间】:2018-02-12 04:55:32
【问题描述】:

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

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


有没有办法创建一个考虑到这一事实的模型,并且可以分配正确的类型来强制转换值('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)

【问题讨论】:

    标签: arrays json swift swift4 codable


    【解决方案1】:

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

    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,感谢您提供此解决方案。是否有您可以推荐的资源(书籍、网站、论坛)涵盖/教授更多有关此类“非标准”问题的信息?我是新手,如果有任何信息可以让我更好地了解如何获得这些解决方案,我将不胜感激。谢谢
    猜你喜欢
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多