简体   繁体   English

如何使用Swift 4 Codable处理JSON格式不一致的内容?

[英]How to handle JSON format inconsistencies with Swift 4 Codable?

I need to parse JSON of which one of the field value is either an array: 我需要解析其中一个字段值是数组的JSON:

"list" :
[
    {
        "value" : 1
    }
]

or an empty string, in case there's no data: 或一个空字符串,以防没有数据:

"list" : ""

Not nice, but I can't change the format. 不好,但是我不能更改格式。

I'm looking at converting my manual parsing, for which this was easy, to JSONDecoder and Codable struct 's. 我正在考虑将我的手动解析(这很容易)转换为JSONDecoderCodable struct

How can I handle this nasty inconsistency? 我该如何处理这种令人讨厌的不一致?

You need to try decoding it one way, and if that fails, decode it the other way. 您需要尝试以一种方式对其进行解码,如果失败,则以另一种方式进行解码。 This means you can't use the compiler-generated decoding support. 这意味着您不能使用编译器生成的解码支持。 You have to do it by hand. 您必须手动完成。 If you want full error checking, do it like this: 如果要进行全面的错误检查,请执行以下操作:

import Foundation

struct ListItem: Decodable {
    var value: Int
}

struct MyResponse: Decodable {

    var list: [ListItem] = []

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        do {
            list = try container.decode([ListItem].self, forKey: .list)
        } catch {
            switch error {
            // In Swift 4, the expected type is [Any].self, but I think it will be [ListItem].self in a newer Swift with conditional conformance support.
            case DecodingError.typeMismatch(let expectedType, _) where expectedType == [Any].self || expectedType == [ListItem].self:
                let dummyString = try container.decode(String.self, forKey: .list)
                if dummyString != "" {
                    throw DecodingError.dataCorruptedError(forKey: .list, in: container, debugDescription: "Expected empty string but got \"\(dummyString)\"")
                }
                list = []
            default: throw error
            }
        }
    }

    enum CodingKeys: String, CodingKey {
        case list
    }

}

If you want no error checking, you can shorten init(from:) to this: 如果您不希望进行错误检查,则可以将init(from:)缩短为:

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        list = (try? container.decode([ListItem].self, forKey: .list)) ?? []
    }

Test 1: 测试1:

let jsonString1 = """
{
    "list" : [ { "value" : 1 } ]
}
"""
print(try! JSONDecoder().decode(MyResponse.self, from: jsonString1.data(using: .utf8)!))

Output 1: 输出1:

MyResponse(list: [__lldb_expr_82.ListItem(value: 1)])

Test 2: 测试2:

let jsonString2 = """
{
    "list" : ""
}
"""
print(try! JSONDecoder().decode(MyResponse.self, from: jsonString2.data(using: .utf8)!))

Output 2: 输出2:

MyResponse(list: [])

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

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