简体   繁体   English

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

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

the data I'm getting from an API returns a single object but when there's multiple objects, it returns an array in the same key.我从 API 获取的数据返回单个对象,但是当有多个对象时,它返回一个包含相同键的数组。 With the current model (struct) I'm working with, the decoding fails when an array shows up.使用我正在使用的当前模型(结构),当数组出现时解码失败。

These results are randomly ordered, meaning I can't know when I will be served an object or array.这些结果是随机排序的,这意味着我不知道什么时候会得到一个对象或数组。


Is there a way to create a model that takes this fact into account and can assign the correct type to cast for the value ('String' or '[String]') so that the decoding continues without problem?有没有办法创建一个模型来考虑这个事实,并且可以分配正确的类型来转换值('String' 或 '[String]'),以便解码继续没有问题?


This is an example of when an object is returned:这是返回对象时的示例:

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

This is an example of when an array is returned (for the same key):这是返回数组时的示例(对于相同的键):

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

Example of the struct that should be able to be used as model to decode both samples above:应该能够用作模型来解码上述两个样本的结构示例:

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

These results are randomly ordered, meaning I can't know when I will be served an object or array.这些结果是随机排序的,这意味着我不知道什么时候会得到一个对象或数组。

Here is the complete playground file:这是完整的游乐场文件:

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)

the data I'm getting from an API returns a single object but when there's multiple objects, it returns an array in the same key.我从 API 获取的数据返回单个对象,但是当有多个对象时,它返回一个包含相同键的数组。 With the current model (struct) I'm working with, the decoding fails when an array shows up.使用我正在使用的当前模型(结构),当数组出现时解码失败。

These results are randomly ordered, meaning I can't know when I will be served an object or array.这些结果是随机排序的,这意味着我不知道什么时候会得到一个对象或数组。


Is there a way to create a model that takes this fact into account and can assign the correct type to cast for the value ('String' or '[String]') so that the decoding continues without problem?有没有办法创建一个模型来考虑这个事实,并且可以分配正确的类型来转换值('String' 或 '[String]'),以便解码继续没有问题?


This is an example of when an object is returned:这是返回对象时的示例:

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

This is an example of when an array is returned (for the same key):这是返回数组时的示例(对于相同的键):

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

Example of the struct that should be able to be used as model to decode both samples above:应该能够用作模型来解码上述两个样本的结构示例:

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

These results are randomly ordered, meaning I can't know when I will be served an object or array.这些结果是随机排序的,这意味着我不知道什么时候会得到一个对象或数组。

Here is the complete playground file:这是完整的游乐场文件:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM