Closed shenfu1991 closed 3 years ago
IF the response array is in a guaranteed format (there are always those 12 fields present, in that order), you can accomplish this by manually decoding from an unkeyedContainer, along the lines of this approach:
struct ResponModel: Decodable {
var value1: Double?
var value2: String?
...
required init(from decoder: Decoder) throws {
var container = try? decoder.unkeyedContainer()
self.value1 = try container.decodeIfPresent(Double.self)
self.value2 = try container.decodeIfPresent(String.self)
...
}
}
IF the response array is in a guaranteed format (there are always those 12 fields present, in that order), you can accomplish this by manually decoding from an unkeyedContainer, along the lines of this approach:
struct ResponModel: Decodable { var value1: Double? var value2: String? ... required init(from decoder: Decoder) throws { var container = try? decoder.unkeyedContainer() self.value1 = try container.decodeIfPresent(Double.self) self.value2 = try container.decodeIfPresent(String.self) ... } }
Thank you very much for your reply, I have successfully decoded.
struct ResponModel: Content{ var value1: Int? var value2: String? var value3: String? var value4: String? var value5: String? var value6: String? var value7: Int? var value8: String? var value9: Int? var value10: String? var value11: String? var value12: String? init(from decoder: Decoder) throws { var container = try decoder.unkeyedContainer() self.value1 = try? container.decodeIfPresent(Int.self) self.value2 = try? container.decodeIfPresent(String.self) self.value3 = try? container.decodeIfPresent(String.self) self.value4 = try? container.decodeIfPresent(String.self) self.value5 = try? container.decodeIfPresent(String.self) self.value6 = try? container.decodeIfPresent(String.self) self.value7 = try? container.decodeIfPresent(Int.self) self.value8 = try? container.decodeIfPresent(String.self) self.value9 = try? container.decodeIfPresent(Int.self) self.value10 = try? container.decodeIfPresent(String.self) self.value11 = try? container.decodeIfPresent(String.self) self.value12 = try? container.decodeIfPresent(String.self) } }
Hello, Why does the client request parsing have to use content? I feel that this is very inefficient and troublesome. I also encountered a problem. How to decode from pure array? There is no way to design a content model.
my server response data is:
i try to write like this:
but the compiler reported an error.