gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 23:00:53 26 4
gpt4 key购买 nike

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

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

这是完整的 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
}
}

关于arrays - Swift 4 JSON Codable - 返回的值有时是一个对象,其他是一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48739760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com