I've the following issue, where I don't get the structure right to decode a response from node-red for the status of my sonos players:
[[{"urlHostname":"192.168.1.1","urlPort":1400,"baseUrl":"http://192.168.1.1:1400","sonosName":"Sonos1","uuid":"RINCON_SONOS14001400","invisible":false},{"urlHostname":"192.168.1.2","urlPort":"1400","baseUrl":"http://192.168.1.2:1400","sonosName":"Sonos2","uuid":"RINCON_SONOS21400","invisible":false},{"urlHostname":"192.168.1.3","urlPort":"1400","baseUrl":"http://192.168.1.3:1400","sonosName":"Sonos3","uuid":"RINCON_SONOS31400","invisible":false},{"urlHostname":"192.168.1.4","urlPort":"1400","baseUrl":"http://192.168.1.4:1400","sonosName":"Sonos4","uuid":"RINCON_SONOS41400","invisible":false},{"urlHostname":"192.168.1.5","urlPort":"1400","baseUrl":"http://192.168.1.5:1400","sonosName":"Sonos5","uuid":"RINCON_SONOS51400","invisible":false},{"urlHostname":"192.168.1.6","urlPort":"1400","baseUrl":"http://192.168.1.6:1400","sonosName":"Sonos6","uuid":"RINCON_SONOS61400","invisible":false}]]
My structure & the call to decode looks as follows:
typealias Response = [sonosData]
struct sonosData: Codable {
let sonos: [sonosStatusEntry]?
}
struct sonosStatusEntry: Codable {
let status: [sonosStatus]?
}
struct sonosStatus: Codable {
let urlHostname: String
let urlPort: Int
let baseUrl: String
let sonosName: String
let uuid: String
let invisible: Bool
}
let response = try JSONDecoder().decode(Response.self, from: data)
I get the following error in Swift:
Failed to load: The data couldn’t be read because it isn’t in the correct format.
Any suggestions?