I'm getting the following error when trying to decode JSON. What's odd is I've used similar code on other endpoints and no issue using UUID.
error:
keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "id", intValue: nil) ("id").", underlyingError: nil))
Here's my class
class Api: ObservableObject {
@State private var showingAlert = false
// completion handler for JSON Data
func getUserData(url: String, completion : @escaping ([RunClubsv2])->()){
let session = URLSession(configuration: .default)
session.dataTask(with: URL(string: url)!) { (data, _, err) in
if err != nil{
print(err!.localizedDescription)
return
}
//decoding JSON
do {
let users = try JSONDecoder().decode([RunClubsv2].self, from: data!)
print(users)
//returning data
completion(users)
}
catch{
print(error)
}
}
.resume()
}
Here's my UI code
@State var runclubv2: [RunClubsv2] = []
var body: some View {
VStack {
if runclubv2.isEmpty{
Text("damn it")
}
else {
//display data
List(runclubv2) { runclubv2 in
Text(runclubv2.name)
}
}
}
.onAppear{
Api().getUserData(url: "<URL EndPoint>") { ([RunClubsv2]) in
self.runclubv2 = self.runclubv2
}
}
}
}
Here's the response data
[
{
"name": "Joggers for Lagers",
"location": "Amor Artis Brewery, Fort Mill",
"date": "6:30 PM",
"category": "Monday"
},
{
"name": "Old Armor Run Club",
"location": "Kannapolis",
"date": "6:00 PM",
"category": "Monday"
}
]