I still learning this Codable/Decodable
new in Swift 4 for JSON parsing.
I am able to build a struct and grab the temperature keys but the Pump and othe
The value for key circuit
is a dictionary.
If you need fast access by number decode the object as [String:Circuit]
public struct ServerReponse: Codable {
let temperature: Temperature
let circuit: [String:Circuit]
}
If an array is more suitable write a custom initializer which additionally maps the values to an array.
public struct ServerReponse: Codable {
let temperature: Temperature
let circuits: [Circuit]
private enum CodingKeys: String, CodingKey { case temperature, circuits = "circuit"}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
temperature = try container.decode(Temperature.self, forKey: .temperature)
let circuitData = try container.decode([String:Circuit].self, forKey: .circuits)
circuits = Array(circuitData.values).sorted(by: {$0.number < $1.number})
}
}