Here, JSON data is Array
of Objects
.
change
try decoder.decode(User.self, from: data)
to
try decoder.decode(Array<User>.self, from: data)
Example:
var users = [User]()
let data = """
[{"U_ID":"1","Email":"test","Password":"test","UserType":"Teacher"}]
""".data(using: .utf8)
do{
users = try JSONDecoder().decode(Array<User>.self, from: data!)
}catch{
print(error.localizedDescription)
}
print(users.first?.Email)
Note: For better understanding, this is my video series about JSON parsing in swift 4