1

Here is my JSON I am trying to parse

{
  "rows": [
    {
      "layout": "Y",
    },
    {
      "layout": "A",
    }
  ]
}

I want to be able to filter out the layout types that are not supported. I am using the JSONDecoder to to convert the JSON data into structs but I am having an issue processing the rows. I am attempting to convert each row using let row = try? rowContainer.decode(Row.self) but I can figure out how to move to the next one if it fails unless I decode it to an empty struct. I tried just decoding it to a Dictionary but it won't except Any for the value field.

enum RowType: String, Codable {
    case a = "A"
    case b = "B"
}

public struct Page: Codable {

    let rows: [Row]

    public init(from decoder: Decoder) throws {

        // Get Container
        let container = try decoder.container(keyedBy: CodingKeys.self)

        // Create Result Array
        var rowResult: [Row] = []

        // Get Rows Container
        var rowContainer = try container.nestedUnkeyedContainer(forKey: .rows)

        // Process Rows
        while !rowContainer.isAtEnd {

            // Create Row
            if let row = try? rowContainer.decode(Row.self) {
                rowResult.append(row)
            }
            else {

                // Increment Row Container
                _ = try rowContainer.decode(Empty.self)
            }
        }

        // Set Result
        rows = rowResult
    }

    // Used For Unsupported Rows
    struct Empty: Codable {}
}

public struct Row: Codable {
    let layout: RowType
}
4

1 回答 1

4

这是一种不同的方法。

layout使用字符串值创建一个临时结构

public struct RawRow: Codable {
    let layout: String
}

Page第一次解码rowsRawRow然后compactMap数组到Row. 它过滤所有layout值不能转换为枚举的项目

public struct Page: Codable {
    let rows: [Row]

    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let rowData = try container.decode([RawRow].self, forKey: .rows)
        rows = rowData.compactMap {
            guard let rowType = RowType(rawValue: $0.layout) else { return nil }
            return Row(layout: rowType)
        }
    }
}
于 2018-07-11T17:23:46.707 回答