I want to get the information of the networks list and decode it but I failed,
💥 fetchNetworks() typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
My Json Data look like this,
{ "networks" : [{
"isSplit" : true,
"securityMode" : "WPA2_PSK",
"ssid" : "Guest5531FC",
"frequencyBand" : "5_GHZ",
"enabled" : true,
"connectedDevices" : 0,
"isGuest" : true,
"password" : "Admin@123",
"isBackhaul" : false,
"visible" : true,
"id" : "@cred4"
},
{
"isSplit" : true,
"securityMode" : "NONE",
"ssid" : "Office_telco",
"frequencyBand" : "2_4_GHZ",
"enabled" : true,
"connectedDevices" : 0,
"isGuest" : false,
"password" : "Admn@234",
"isBackhaul" : false,
"visible" : true,
"id" : "@cred0"
}]}
My Source Code is like,
import Foundation
struct WanNetworks: Codable, Equatable {
let networks: [Networks]
}
import Foundation
struct Networks: Codable, Equatable {
let connectedDevices: Int
let id: String
let enabled: Bool
let visible: Bool
let isGuest: Bool
let ssid: String
let bssid: String?
let frequencyBands: String?
let isBackhaul: Bool
let frequencyBand: String
let password: String?
let securityMode: String
let isSplit: Bool
}
import Foundation
/// The serializer is responsible for generating the response that is returned by the service request.
public protocol Serializer {
/// The response returned by the `execute`'s `completion` handler.
associatedtype Response
/// - returns: A generic response defined by the implementation class.
func serialize(data: Data?, error: Error?, response: HTTPURLResponse?) -> Response
}
import Foundation
public struct CodableSerializer<Model: Codable>: Serializer {
// MARK: - Internal
private var decoder: JSONDecoder
// MARK: - Init
public init(decoder: JSONDecoder = JSONDecoder()) {
self.decoder = decoder
}
// MARK: - Response
/// The codable response returned by the `CodableSerializer`.
public enum Response {
/// When data is correctly parsed we return the `Codable` instance. This can also be nil when no
/// data was returned.
case success(Model?)
/// Failure is triggered an error is returned or when parsing the data didn't succeed.
case failure(Error)
}
// MARK: - Serialize
public func serialize(data: Data?, error: Error?, response: HTTPURLResponse?) -> CodableSerializer<Model>.Response {
// When an error occurs we return this error.
if let error = error { return .failure(error) }
// When a status code larger or equal than 400 is returned we return a custom error.
if
let response = response,
let responseError = ResponseError(statusCode: response.statusCode) {
print(response.statusCode)
return .failure(responseError)
}
/// When no data object (or an empty one) is returned from the server we have a successful.
guard
let data = data,
!data.isEmpty else { return .success(nil) }
print("datas is ",data)
do {
print(Model.self)
let result = try decoder.decode(Model.self, from: data)
return .success(result)
} catch {
return .failure(error)
}
}
}
Xcode always giving me the error "Expected to decode Array but found a dictionary instead". I don't understand what it mean here. More precisely, I did not understand what to do. I used the same code to pull other information from JSON and it worked there. I looked at the answers to the questions asked under this title before, but I could not get an answer to my question. How can I fix this?